11.1 cp: 複製文件和目錄
下一篇: dd invocation, 上一级: 基本操作 [目錄][索引]
11.1 cp: 複製文件和目錄
cp 命令建立一个文件(或目录)的副本。副本完全独立于原来的文件。您可以复制一个文件为另外一个,也可以将任意多的文件复制到指定的目录里。语法:
cp [option]… [-T] source dest
cp [option]… source… directory
cp [option]… -t directory source…
- 如果给定了两個文件,
cp命令將第一個文件複製到第二個文件。 - 如果指定了参数 --target-directory ,或者最后是一个目录名而且没有指定參數 --no-target-directory (-T)。 那么
cp命令將會使用 源文件 中的文件名在目标目錄里建立一份副本。
通常情况,文件的寫入文件的格式與读取文件的格式一致,例外的情况參閱下面的 --sparse 參數。
默認情况, cp 命令不複製目錄。但是, 參數-R, -a, 和 -r 會使 cp 命令以递归的方式將源目錄中的文件複製到對應的目标目錄里。
當複製的是一個符號链接時, cp 通常會跟随複製该链接指向的地方。只有在使用递归或指定了参数 --link (-l) 时才会复该该链接。這一項是默认的設置,可以通過使用參數 --archive (-a), -d, --dereference (-L), --no-dereference (-P), 和 -H 来改變其默認的設置。如果同時指定了多個參數,那么最后一個參數会覆盖其它的參數。
只有当一个符号链接指向一个现有的常规文件时 cp 命令才会对其进行 复制。如果符号链接处于悬空状态时 cp 命令默认不对其进行操作,并且 在诊断时失败。因为这种操作是危险的操作,违反了历史惯例和 POSIX。尽管如此,你还 是可以通过 POSIXLY_CORRECT 来使 cp 复制一个悬空的符 号链接。另外 --backup 或 --link 这样的选项在复制前重 命名或删除目标时, cp 只会删除符号链接本身,不会删除所指向的文件。
By default, cp copies the contents of special files only when not copying recursively. This default can be overridden with the --copy-contents option.
cp generally refuses to copy a file onto itself, with the following exception: if --force --backup is specified with source and dest identical, and referring to a regular file, cp will make a backup file, either regular or numbered, as specified in the usual ways (see Backup options). This is useful when you simply want to make a backup of an existing file before changing it.
這個命令可以接收以下給定的參數。也可參考 通用選項。
- ‘-a’
- ‘--archive’
-
Preserve as much as possible of the structure and attributes of the original files in the copy (but do not attempt to preserve internal directory structure; i.e., ‘ls -U’ may list the entries in a copied directory in a different order). Try to preserve SELinux security context and extended attributes (xattr), but ignore any failure to do that and print no corresponding diagnostic. Equivalent to -dR --preserve=all with the reduced diagnostics.
- ‘--attributes-only’
-
Copy only the specified attributes of the source file to the destination. If the destination already exists, do not alter its contents. See the --preserve option for controlling which attributes to copy.
- ‘-b’
- ‘--backup[=method]’
-
參閱 Backup options。 對每一個將要被覆盖或删除的文件建立一份副本。 As a special case,
cpmakes 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. One useful application of this combination of options is this tiny Bourne shell script:#!/bin/sh # Usage: backup FILE... # Create a GNU-style backup of each listed FILE. fail=0 for i; do cp --backup --force --preserve=all -- "$i" "$i" || fail=1 done exit $fail - ‘--copy-contents’
-
If copying recursively, copy the contents of any special files (e.g., FIFOs and device files) as if they were regular files. This means trying to read the data in each source file and writing it to the destination. It is usually a mistake to use this option, as it normally has undesirable effects on special files like FIFOs and the ones typically found in the /dev directory. In most cases,
cp -R --copy-contentswill hang indefinitely trying to read from FIFOs and special files like /dev/console, and it will fill up your destination disk if you use it to copy /dev/zero. This option has no effect unless copying recursively, and it does not affect the copying of symbolic links. - ‘-d’
-
將符號链接作为一個符號链接进行複製,而不是複製符號链接指向的地方,并在副本裡与源文件之间保持硬链接。相當於 --no-dereference --preserve=links。
- ‘-f’
- ‘--force’
-
當没有這個參數时,目標文件不能被打开或不能被寫入時就會失败。如果使用參數 --force ,當目標文件无法打开時
cp會尝试删除该文件然後再重新創建该文件。當该參數与參數 --link (-l) 或 --symbolic-link (-s) 一起使用時,目標链接將會被替换,and unless --backup (-b) is also given there is no brief moment when the destination does not exist. Also see the description of --remove-destination.This option is independent of the --interactive or -i option: neither cancels the effect of the other.
This option is ignored when the --no-clobber or -n option is also used.
- ‘-H’
-
If a command line argument specifies a symbolic link, then copy the file it points to rather than the symbolic link itself. However, copy (preserving its nature) any symbolic link that is encountered via recursive traversal.
- ‘-i’
- ‘--interactive’
-
覆盖前询问,-i 前面的 -n 参数会失效。
- ‘-l’
- ‘--link’
-
對源文件建立硬链接,而非建立文件的副本。
- ‘-L’
- ‘--dereference’
-
Follow symbolic links when copying from them. With this option,
cpcannot create a symbolic link. For example, a symlink (to regular file) in the source tree will be copied to a regular file in the destination tree. - ‘-n’
- ‘--no-clobber’
-
不要覆盖已存在的文件。 -n 参数會使前面的参数-i 失效。這個選項與 -b 或--backup 參數不能同時使用。
- ‘-P’
- ‘--no-dereference’
-
將符号链接复制为符号链接,而不是复制链接所指向的文件。這個選項只影响源目錄裡的链接,如果有效目标文件中的链接會被自动更新。
- ‘-p’
- ‘--preserve[=attribute_list]’
-
保留原始文件指定的属性。如果要指定属性 attribute_list 必须是一个字符串或者多个字符串中间用逗号作为分隔的列表:
- ‘mode’
-
保存文件模式位和访问控制列表。
- ‘ownership’
-
保持归属的组。在大多数的现代系统中,只有拥有权限的用户才能更改文件的所有者,而普通用户只有在所需的文件恰巧与自己在同一个组里才能保留文件的归属组。
- ‘timestamps’
-
保持最后的访问时间和最后昏修改时间。在老系统上,当复制的是一个符号链接时是不能保持该属性不变的。然而,许多现在的系统提供
utimensat函数功能,这使得想要保持符号链接的时间戳不变也成了可能。 - ‘links’
-
將目标中的所有文件与源文件之间建立链接。注意,使用 -L 或 -H 参数會将符号链接转换为硬链接。例如:
$ mkdir c; : > a; ln -s a b; cp -aH a b c; ls -i1 c 74161745 a 74161745 b注意输入: b 是常规文件 a 的硬链接。但是目标目录 c 中却是一个硬链接。 因为 a 暗含有 --no-dereference 它会复制这个链接,但是后面的参数 -H 告诉
cp取消这个链接跟综它,然后它会看到两个相同的文件索引节点编号。接着 --preserve=links 参数也暗示了 -a 保存找到的硬链接。这里有一个类似的例子,可以用来练习
cp命令的 -L 参数:$ mkdir b c; (cd b; : > a; ln -s a b); cp -aL b c; ls -i1 c/b 74163295 a 74163295 b - ‘context’
-
Preserve SELinux security context of the file, or fail with full diagnostics.
- ‘xattr’
-
Preserve extended attributes of the file, or fail with full diagnostics. If
cpis built without xattr support, ignore this option. If SELinux context, ACLs or Capabilities are implemented using xattrs, they are preserved implicitly by this option as well, i.e., even without specifying --preserve=mode or --preserve=context. - ‘all’
-
保持文件的所有属性。相當于上面所指的所有属性,but with the difference that failure to preserve SELinux security context or extended attributes does not change
cp’s exit status. In contrast to -a, all but ‘Operation not supported’ warnings are output.
Using --preserve with no attribute_list is equivalent to --preserve=mode,ownership,timestamps.
In the absence of this option, the permissions of existing destination files are unchanged. Each new file is created with the mode of the corresponding source file minus the set-user-ID, set-group-ID, and sticky bits as the create mode; the operating system then applies either the umask or a default ACL, possibly resulting in a more restrictive file mode. See File permissions.
- ‘--no-preserve=attribute_list’
-
Do not preserve the specified attributes. The attribute_list has the same form as for --preserve.
- ‘--parents’
-
Form the name of each destination file by appending to the target directory a slash and the specified name of the source file. The last argument given to
cpmust be the name of an existing directory. For example, the command:cp --parents a/b/c existing_dircopies the file a/b/c to existing_dir/a/b/c, creating any missing intermediate directories.
- ‘-R’
- ‘-r’
- ‘--recursive’
-
递归复制目錄。By default, do not follow symbolic links in the source unless used together with the --link (-l) option; see the --archive (-a), -d, --dereference (-L), --no-dereference (-P), and -H options. Special files are copied by creating a destination file of the same type as the source; see the --copy-contents option. It is not portable to use -r to copy symbolic links or special files. On some non-GNU systems, -r implies the equivalent of -L and --copy-contents for historical reasons. Also, it is not portable to use -R to copy symbolic links unless you also specify -P, as POSIX allows implementations that dereference symbolic links by default.
- ‘--reflink[=when]’
-
Perform a lightweight, copy-on-write (COW) copy, if supported by the file system. Once it has succeeded, beware that the source and destination files share the same disk data blocks as long as they remain unmodified. Thus, if a disk I/O error affects data blocks of one of the files, the other suffers the same fate.
The when value can be one of the following:
- ‘always’
-
The default behavior: if the copy-on-write operation is not supported then report the failure for each file and exit with a failure status.
- ‘auto’
-
If the copy-on-write operation is not supported then fall back to the standard copy behavior.
This option is overridden by the --link, --symbolic-link and --attributes-only options, thus allowing it to be used to configure the default data copying behavior for
cp. For example, with the following alias,cpwill use the minimum amount of space supported by the file system.alias cp='cp --reflink=auto --sparse=always' - ‘--remove-destination’
-
Remove each existing destination file before attempting to open it (contrast with -f above).
- ‘--sparse=when’
-
A sparse file contains holes—a sequence of zero bytes that does not occupy any physical disk blocks; the ‘read’ system call reads these as zeros. This can both save considerable disk space and increase speed, since many binary files contain lots of consecutive zero bytes. By default,
cpdetects holes in input source files via a crude heuristic and makes the corresponding output file sparse as well. Only regular files may be sparse.The when value can be one of the following:
- ‘auto’
-
The default behavior: if the input file is sparse, attempt to make the output file sparse, too. However, if an output file exists but refers to a non-regular file, then do not attempt to make it sparse.
- ‘always’
-
For each sufficiently long sequence of zero bytes in the input file, attempt to create a corresponding hole in the output file, even if the input file does not appear to be sparse. This is useful when the input file resides on a file system that does not support sparse files (for example, ‘efs’ file systems in SGI IRIX 5.3 and earlier), but the output file is on a type of file system that does support them. Holes may be created only in regular files, so if the destination file is of some other type,
cpdoes not even try to make it sparse. - ‘never’
-
Never make the output file sparse. This is useful in creating a file for use with the
mkswapcommand, since such a file must not have any holes.
- ‘--strip-trailing-slashes’
-
Remove any trailing slashes from each source argument. See Trailing slashes.
- ‘-s’
- ‘--symbolic-link’
-
Make symbolic links instead of copies of non-directories. All source file names must be absolute (starting with ‘/’) unless the destination files are in the current directory. This option merely results in an error message on systems that do not support symbolic links.
- ‘-S suffix’
- ‘--suffix=suffix’
-
對每一個使用 -b 参数备份的文件添加 后缀 。 参阅 Backup options。
- ‘-t directory’
- ‘--target-directory=directory’
-
Specify the destination directory. See Target directory.
- ‘-T’
- ‘--no-target-directory’
-
Do not treat the last operand specially when it is a directory or a symbolic link to a directory. See Target directory.
- ‘-u’
- ‘--update’
-
只在源文件比目标文件新,或目标文件不存在时才进行复制。If timestamps are being preserved, the comparison is to the source timestamp truncated to the resolutions of the destination file system and of the system calls used to update timestamps; this avoids duplicate work if several ‘cp -pu’ commands are executed with the same source and destination. If --preserve=links is also specified (like with ‘cp -au’ for example), that will take precedence. Consequently, depending on the order that files are processed from the source, newer files in the destination may be replaced, to mirror hard links in the source.
- ‘-v’
- ‘--verbose’
-
顯示每一個將要被複製文件的名字。
- ‘-x’
- ‘--one-file-system’
-
複製时跳過與當前文件系統不同的子目录(即不处理其它分区的文件)。 However, mount point directories are copied.
- ‘-Z’
- ‘--context[=context]’
-
Without a specified context, adjust the SELinux security context according to the system default type for destination files, similarly to the
restoreconcommand. The long form of this option with a specific context specified, will set the context for newly created files only. With a specified context, if both SELinux and SMACK are disabled, a warning is issued. This option is mutually exclusive with the --preserve=context option, and overrides the --preserve=all and -a options.
退出狀态为零表示成功,而非零表示失敗。
下一篇: dd invocation, 上一级: 基本操作 [目錄][索引]

浙公网安备 33010602011771号