Go to main content

手册页部分 1:用户命令

退出打印视图

更新时间: 2022年7月27日 星期三
 
 

cp(1)

名称

cp - copy files

用法概要

/usr/bin/cp [-Pfipz@/] source_file target_file
/usr/bin/cp [-Pfipz@/] source_file... target
/usr/bin/cp -r [-H | -L | -P] [-fipz@/] source_dir... target
/usr/bin/cp -R [-H | -L | -P] [-fipz@/] source_dir... target
/usr/xpg4/bin/cp [-Pfipz@/] source_file target_file
/usr/xpg4/bin/cp [-Pfipz@/] source_file... target
/usr/xpg4/bin/cp -r [-H | -L | -P] [-fipz@/] source_dir... target
/usr/xpg4/bin/cp -R [-H | -L | -P] [-fipz@/] source_dir... target

描述

In the first synopsis form, neither source_file nor target_file are directory files, nor can they have the same name.The cp utility copies the contents of source_file to the destination path named by target_file.如果 target_file 存在,cp 将覆盖其内容,但是与其关联的模式(和 ACL,如果适用)、所有者和组不会改变。The last modification time of target_file and the last access time of source_file are set to the time the copy was made.If target_file does not exist, cp creates a new file named target_file that has the same mode as source_file except that the sticky bit is not set unless the user has the {PRIV_SYS_CONFIG} privilege.在这种情况下,除非为包含新建文件的目录设置 setgid 位,否则 target_file 的所有者和组即为用户的所有者和组。如果设置了目录的 setgid 位,则新建文件的组是包含该文件的目录的组而非创建该文件的用户的组。如果 target_file 是指向另一个文件的链接,cp 将以 source_file 的内容覆盖链接目标;target_file 中的链接保持不变。

在第二种用法概要形式中,将一个或多个 source_file 复制到 target 指定的目录。如果 target 不存在或不是目录,而某个 source_file 的文件类型是目录,则会出现错误。

在第三种或第四种用法概要形式中,将 source_dir 指定的一个或多个目录复制到 target 指定的目录。必须指定 –r 或 –R。对于每个 source_dir,cp 将复制所有文件和子目录。

选项

The following options are supported for both /usr/bin/cp and /usr/xpg4/bin/cp:

–f

Unlink.如果无法获取目标文件的文件描述符,该选项将尝试解除目标文件的链接并继续。

–H

根据指定为 source_file 操作数的任何符号链接引用的文件的类型和内容执行操作。

如果 source_file 操作数是符号链接,则 cp 将复制 source_file 操作数的符号链接引用的文件。遍历文件分层结构期间遇到的其他所有符号链接都将保留。

–i

交互。每当复制操作要覆盖现有目标时,cp 都会提示用户确认。无论是否从终端输入,都会执行该操作。如果提示确认失败,等效于用户答复否定。肯定性响应意味着应该继续复制。Any other answer prevents cp from overwriting target.

–L

根据指定为 source_file 操作数的任何符号链接或遍历文件分层结构期间遇到的任何符号链接引用的文件的类型和内容执行操作。

复制符号链接引用的文件。遍历文件分层结构期间遇到的符号链接不予保留。

–p

Preserve.cp 实用程序不仅复制 source_file 的内容,而且还尝试保留其 ACL、访问和修改时间、扩展属性、扩展系统属性、文件模式以及所有者和组 ID。

如果 cp 无法保留访问和修改时间、扩展属性或文件模式,cp 不会视为操作失败。如果 cp 无法保留所有者和组 ID,复制操作不算失败,但 cp 会无提示地将 S_ISUID 和 S_ISGID 位从目标的文件模式中清除。如果 cp 无法清除这些位,则复制失败。如果 cp 无法保留 ACL 或扩展系统属性,则复制失败。If the copy fails, then a diagnostic message is written to stderr and (after processing any remaining operands) cp exits with a non-zero exit status.

–P

Takes actions on any symbolic link specified as a source_file operand or any symbolic link encountered during traversal of a file hierarchy.

Copies symbolic links.遍历文件分层结构期间遇到的符号链接予以保留。

–r

Recursive. cp copies the directory and all its files, including any subdirectories and their files to target.除非指定 –H、–L 或 –P 选项,否则 –L 选项将用作缺省模式。

–R

与 –r 相同,但复制管道而不是从其读取。

–z

快速复制。cp 将对源文件和目标文件执行 reflink 操作。有关更多信息,请参见 reflink(3C) 手册页。

–@

保留扩展属性。cp 将尝试将源文件的所有扩展属性连同文件数据复制到目标文件。

–/

保留扩展属性和扩展系统属性。cp 实用程序将尝试将每个源文件的扩展属性和扩展系统属性,以及与扩展属性关联的扩展系统属性连同文件数据复制到目标文件。If cp is unable to copy extended attributes or extended system attributes, then a diagnostic message is written to stderr and (after processing any remaining operands) exits with a non-zero exit status.

Specifying more than one of the mutually-exclusive options –H, –L, and –P is not considered an error.指定的最后一个选项将决定实用程序的行为。

/usr/bin/cp

如果在指定 –p 选项的同时也指定 –@ 选项或 –/ 选项,则 /usr/bin/cp 行为方式如下:

  • –p 和 –@ 都以任意顺序指定时,如果无法复制扩展属性,则复制失败。

  • –p 和 –/ 都以任意顺序指定时,如果无法复制扩展系统属性,则复制失败。

/usr/xpg4/bin/cp

If the –p option is specified with either the –@ option or the –/ option, /usr/xpg4/bin/cp behaves as follows:

  • 同时指定 –p 和 –@ 时,如果无法保留扩展属性,指定的最后一个选项将决定复制是否失败。

  • 同时指定 –p 和 –/ 时,如果无法保留扩展系统属性,指定的最后一个选项将决定复制是否失败。

操作数

支持下列操作数:

source_file

要复制的正规文件的路径名。

source_dir

要复制的目录的路径名。

target_file

现有或不存在的文件的路径名,在复制单个文件时用于输出。

target

要包含复制的文件的目录的路径名。

示例

示例 1 复制文件

以下命令将复制文件:

example% cp goodies goodies.old

example% ls goodies*
goodies goodies.old
示例 2 复制文件列表

以下命令会将文件列表复制到目标目录:

example% cp ~/src/* /tmp
示例 3 复制目录

以下命令首先将目录复制到新目标目录,然后将其复制到现有目标目录

example% ls ~/bkup
/usr/example/fred/bkup not found

example% cp -r ~/src ~/bkup

example% ls -R ~/bkup
x.c y.c z.sh

example% cp -r ~/src ~/bkup

example% ls -R ~/bkup
src x.c y.c z.sh
src:
x.c y.c z.s
示例 4 复制扩展文件系统属性

以下命令将复制扩展文件系统属性:

$ ls -/ c file1
-rw-r--r--   1 foo   staff          0 Oct 29 20:04 file1
                {AH-----m--}

$ cp -/ file1 file2
$ ls -/c file2
-rw-r--r--   1 foo  staff          0 Oct 29 20:17 file2
                {AH-----m--}
示例 5 无法复制扩展系统属性

以下命令将无法复制扩展系统属性:

$ ls -/c file1
-rw-r--r--   1 foo    staff          0 Oct 29 20:04 file1
                {AH-----m--}

$ cp -/ file1 /tmp
cp: Failed to copy extended system attributes from file1 to /tmp/file1

$ ls -/c /tmp/file1
-rw-r--r--   1 foo    staff          0 Oct 29 20:09 /tmp/file1
                {}

环境变量

See environ(7) for descriptions of the following environment variables that affect the execution of cp: LANG, LC_ALL, LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.

肯定的回答使用为用户语言环境中 LC_MESSAGES 类别的 yesexpr 关键字定义的扩展正则表达式进行处理。LC_COLLATE 类别中指定的语言环境定义为 yesexpr 定义的表达式中使用的范围、等效类和多字符整理元素的行为。LC_CTYPE 中指定的语言环境确定文本数据字符字节序列解释的语言环境,以及为 yesexpr 定义的表达式中使用的字符类的行为。请参见 locale(7)。

退出状态

将返回以下退出值:

0

所有文件都已成功复制。

> 0

出现错误。

属性

有关下列属性的说明,请参见 attributes(7):

/usr/bin/cp

属性类型
属性值
可用性
system/core-os
CSI
Enabled(已启用)
接口稳定性
Committed(已确定)

/usr/xpg4/bin/cp

属性类型
属性值
可用性
system/xopen/xcu4
CSI
Enabled(已启用)
接口稳定性
Committed(已确定)

另请参见

chmod(1), chown(1), utime(2), fgetattr(3C), attributes(7), environ(7), fsattr(7), locale(7), privileges(7), standards(7)

附注

源文件的权限模式将在副本中保留。

用户可以使用 -- 显式标记任何命令行选项的结束,以使 cp 能够识别以 - 开头的文件名参数。