MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 12478|回复: 2
打印 上一主题 下一主题

[Linux命令] cp命令详解及举例

[复制链接]
跳转到指定楼层
1#
发表于 2007-8-15 15:30:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
该命令的功能是将给出的文件或目录拷贝到另一文件或目录中,同MSDOS下的copy命令一样,功能十分强大。

语法:cp [选项] 源文件或目录 目标文件或目录
说明:该命令把指定的源文件复制到目标文件或把多个源文件复制到目标目录中。

该命令的各选项含义如下:
- a 该选项通常在拷贝目录时使用。它保留链接、文件属性,并递归地拷贝目录,其作用等于dpR选项的组合。
- d 拷贝时保留链接。
- f 删除已经存在的目标文件而不提示。
- i 和f选项相反,在覆盖目标文件之前将给出提示要求用户确认。回答y时目标文件将被覆盖,是交互式拷贝。
- p 此时cp除复制源文件的内容外,还将把其修改时间和访问权限也复制到新文件中。
- r 若给出的源文件是一目录文件,此时cp将递归复制该目录下所有的子目录和文件。此时目标文件必须为一个目录名。
- l 不作拷贝,只是链接文件。

需要说明的是,为防止用户在不经意的情况下用cp命令破坏另一个文件,如用户指定的目标文件名已存在,用cp命令拷贝文件后,这个文件就会被新源文件覆盖,因此,建议用户在使用cp命令拷贝文件时,最好使用i选项。

例子:
cp -i file1 file2 (将文档 file1 复制成 file2 . –i为提示确认。)

cp file1 dir1 将文档 file1 复制到目录 dir1 下,文件名仍为 file1.

cp /tmp/file1 . 将目录 /tmp 下的文档 file1复制到现行目录下,档名仍为 file1.

cp /tmp/file1 file2 将目录 /tmp 下的文档 file1现行目录下,档名为file2

cp -r dir1 dir2 (recursive copy) 复制整个目录.若目录 dir2 不存在,则将目录dir1,及其所有文档和子目录,复制到目录 dir2 下,新目录名称为dir1.若目录dir2不存在,则将dir1,及其所有文档和子目录,复制为目录 dir2


Linux中对cp命令的帮助输出:
[root@DevDBServer]# cp --help
Usage: cp [OPTION]... SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... --target-directory=DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
  -a, --archive                same as -dpR
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
      --copy-contents          copy contents of special files when recursive
  -d                           same as --no-dereference --preserve=link
      --no-dereference         never follow symbolic links
  -f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again
  -i, --interactive            prompt before overwrite
  -H                           follow command-line symbolic links
  -l, --link                   link files instead of copying
  -L, --dereference            always follow symbolic links
  -p                           same as --preserve=mode,ownership,timestamps
      --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                 mode,ownership,timestamps), if possible
                                 additional attributes: links, all
  -c                           same as --preserve=context
      --no-preserve=ATTR_LIST  don't preserve the specified attributes
      --parents                append source path to DIRECTORY
  -P                           same as `--no-dereference'
  -R, -r, --recursive          copy directories recursively
      --remove-destination     remove each existing destination file before
                                 attempting to open it (contrast with --force)
      --reply={yes,no,query}   specify how to handle the prompt about an
                                 existing destination file
      --sparse=WHEN            control creation of sparse files
      --strip-trailing-slashes remove any trailing slashes from each SOURCE
                                 argument
  -s, --symbolic-link          make symbolic links instead of copying
  -S, --suffix=SUFFIX          override the usual backup suffix
      --target-directory=DIRECTORY  move all SOURCE arguments into DIRECTORY
  -u, --update                 copy only when the SOURCE file is newer
                                 than the destination file or when the
                                 destination file is missing
  -v, --verbose                explain what is being done
  -x, --one-file-system        stay on this file system
  -Z, --context=CONTEXT        set security context of copy to CONTEXT
      --help     display this help and exit
      --version  output version information and exit
By default, sparse SOURCE files are detected by a crude heuristic and the
corresponding DEST file is made sparse as well.  That is the behavior
selected by --sparse=auto.  Specify --sparse=always to create a sparse DEST
file whenever the SOURCE file contains a long enough sequence of zero bytes.
Use --sparse=never to inhibit creation of sparse files.
The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable.  Here are the values:
  none, off       never make backups (even if --backup is given)
  numbered, t     make numbered backups
  existing, nil   numbered if numbered backups exist, simple otherwise
  simple, never   always make simple backups
As a special case, cp makes a backup of SOURCE when the force and backup
options are given and SOURCE and DEST are the same name for an existing,
regular file.



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享淘帖 顶 踩
2#
发表于 2008-3-26 09:19:28 | 只看该作者
3#
 楼主| 发表于 2009-4-19 16:45:35 | 只看该作者
在RHEL5.2中cp命令的参数是有变化和改进的,如没有了--relay参数...

  1. [root@node01 data]# cp --help
  2. Usage: cp [OPTION]... [-T] SOURCE DEST
  3.   or:  cp [OPTION]... SOURCE... DIRECTORY
  4.   or:  cp [OPTION]... -t DIRECTORY SOURCE...
  5. Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
  6. Mandatory arguments to long options are mandatory for short options too.
  7.   -a, --archive                same as -dpR
  8.       --backup[=CONTROL]       make a backup of each existing destination file
  9.   -b                           like --backup but does not accept an argument
  10.       --copy-contents          copy contents of special files when recursive
  11.   -d                           same as --no-dereference --preserve=link
  12.   -f, --force                  if an existing destination file cannot be
  13.                                  opened, remove it and try again
  14.   -i, --interactive            prompt before overwrite
  15.   -H                           follow command-line symbolic links
  16.   -l, --link                   link files instead of copying
  17.   -L, --dereference            always follow symbolic links
  18.   -P, --no-dereference         never follow symbolic links
  19.   -p                           same as --preserve=mode,ownership,timestamps
  20.       --preserve[=ATTR_LIST]   preserve the specified attributes (default:
  21.                                  mode,ownership,timestamps), if possible
  22.                                  additional attributes: links, all
  23.   -c                           same as --preserve=context
  24.       --no-preserve=ATTR_LIST  don't preserve the specified attributes
  25.       --parents                use full source file name under DIRECTORY
  26.   -R, -r, --recursive          copy directories recursively
  27.       --remove-destination     remove each existing destination file before
  28.                                  attempting to open it (contrast with --force)
  29.       --sparse=WHEN            control creation of sparse files
  30.       --strip-trailing-slashes remove any trailing slashes from each SOURCE
  31.                                  argument
  32.   -s, --symbolic-link          make symbolic links instead of copying
  33.   -S, --suffix=SUFFIX          override the usual backup suffix
  34.   -t, --target-directory=DIRECTORY  copy all SOURCE arguments into DIRECTORY
  35.   -T, --no-target-directory    treat DEST as a normal file
  36.   -u, --update                 copy only when the SOURCE file is newer
  37.                                  than the destination file or when the
  38.                                  destination file is missing
  39.   -v, --verbose                explain what is being done
  40.   -x, --one-file-system        stay on this file system
  41.   -Z, --context=CONTEXT        set security context of copy to CONTEXT
  42.       --help     display this help and exit
  43.       --version  output version information and exit

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-5-9 00:57 , Processed in 0.084035 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表