> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man1/cp.1.md).

# cp.1

`cp` — 复制文件

## 名称

`cp`

## 概要

`cp [-R [-H | -L | -P]] [-f | -i | -n] [-alNpsvx] source_file target_file`

`cp [-R [-H | -L | -P]] [-f | -i | -n] [-alNpsvx] source_file ... target_directory`

`cp [-f | -i | -n] [-alNPpsvx] source_file target_file`

`cp [-f | -i | -n] [-alNPpsvx] source_file ... target_directory`

## 描述

在第一种概要形式中，`cp` 实用程序将 `source_file` 的内容复制到 `target_file`。在第二种概要形式中，每个指定的 `source_file` 的内容被复制到目标 `target_directory`。文件本身的名称不会改变。如果 `cp` 检测到试图将文件复制到自身的操作，复制将失败。

以下选项可用：

**`-H`** 如果指定了 `-R` 选项，则跟随命令行上的符号链接。（遍历目录树时遇到的符号链接不被跟随。）

**`-L`**, **`--dereference`** 如果指定了 `-R` 选项，则跟随所有符号链接。

**`-P`**, **`--no-dereference`** 不跟随任何符号链接。如果指定了 `-R` 选项，这是默认行为。

**`-R`**, **`--recursive`** 如果 `source_file` 指定的是一个目录，`cp` 将复制该目录以及连接在该处的整个子树。如果 `source_file` 以 `/` 结尾，则复制目录的内容而非目录本身。此选项还使得符号链接被复制而非通过间接引用，且 `cp` 会创建特殊文件而非将其作为普通文件复制。创建的目录与相应源目录具有相同的模式，不受进程 umask 修改。注意，`cp` 将硬链接文件作为独立文件复制。如果需要保留硬链接，请考虑改用 [tar(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/tar.1.md)、cpio(1) 或 [pax(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/pax.1.md)。

**`-a`**, **`--archive`** 归档模式。等同于 `-RpP`。

**`-f`**, **`--force`** 对于每个已存在的目标路径名，移除它并创建一个新文件，无论其权限如何都不提示确认。（`-f` 选项覆盖任何先前指定的 `-i` 或 `-n` 选项。）

**`-i`**, **`--interactive`** 在复制会覆盖现有文件之前，向标准错误输出写入提示。如果来自标准输入的响应以字符 ‘`y`’ 或 ‘`Y`’ 开头，则尝试复制文件。（`-i` 选项覆盖任何先前指定的 `-f` 或 `-n` 选项。）

**`-l`**, **`--link`** 在层次结构中创建到常规文件的硬链接而非复制。

**`-N`** 与 `-p` 一起使用时，禁止复制文件标志。

**`-n`**, **`--no-clobber`** 不覆盖现有文件。（`-n` 选项覆盖任何先前指定的 `-f` 或 `-i` 选项。）

**`-p`** 在副本中保留每个源文件的以下属性（在权限允许的情况下）：修改时间、访问时间、文件标志、文件模式、ACL、用户 ID 和组 ID。如果用户 ID 和组 ID 无法保留，不会显示错误消息，退出值也不改变。如果源文件的 set-user-ID 位开启且用户 ID 无法保留，则副本的权限中不保留 set-user-ID 位。如果源文件的 set-group-ID 位开启且组 ID 无法保留，则副本的权限中不保留 set-group-ID 位。如果源文件的 set-user-ID 和 set-group-ID 位都开启，且用户 ID 或组 ID 中任一无法保留，则副本的权限中 set-user-ID 和 set-group-ID 位均不保留。

**`--sort`** 以（非本地化的）字典顺序访问和遍历源。通常，`cp` 按源在命令行上列出的顺序访问，如果递归，则按内核返回的顺序遍历其内容，这可能是创建顺序、字典顺序或其他顺序。使用 `--sort` 时，源按字典顺序访问和遍历。这主要用于测试。

**`-s`**, **`--symbolic-link`** 在层次结构中创建到常规文件的符号链接而非复制。

**`-v`**, **`--verbose`** 详细输出，显示每个文件复制时的源路径和目标路径。

**`-x`**, **`--one-file-system`** 不遍历文件系统挂载点。

对于每个已存在的目标文件，如果权限允许，其内容将被覆盖。除非指定了 `-p` 选项，否则其模式、用户 ID 和组 ID 保持不变。

在第二种概要形式中，`target_directory` 必须存在，除非只有一个指定的 `source_file` 且它是目录并指定了 `-R` 标志。

如果目标文件不存在，则使用源文件的模式，并按文件模式创建掩码（`umask`，参见 [csh(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/csh.1.md)）修改。如果源文件的 set-user-ID 位开启，则该位被移除，除非源文件和目标文件由同一用户拥有。如果源文件的 set-group-ID 位开启，则该位被移除，除非源文件和目标文件属于同一组且用户是该组成员。如果 set-user-ID 和 set-group-ID 位都设置，则必须满足上述所有条件，否则两个位都被移除。

文件创建或覆盖需要适当的权限。

符号链接总是被跟随，除非设置了 `-R` 标志，此时默认不跟随符号链接。`-H` 或 `-L` 标志（与 `-R` 标志结合使用）会使符号链接按上述方式被跟随。除非指定了 `-R` 选项，否则 `-H` 和 `-L` 选项被忽略。`-P` 选项可与 `-R` 一起使用也可不一起使用。这些选项相互覆盖，命令的行为由最后指定的选项决定。

如果 `cp` 收到 `SIGINFO` 信号（参见 stty(1) 的 `status` 参数），当前输入和输出文件以及完成百分比将被写入标准输出。

## 退出状态

`cp` 实用程序成功时退出值为 0，发生错误时大于 0。

## 实例

复制文件 `foo` 为 `bar`：

```sh
$ cp foo bar
```

将一组文件复制到 **/tmp** 目录：

```sh
$ cp *.txt /tmp
```

将目录 `junk` 及其所有内容（包括任何子目录）复制到 **/tmp** 目录：

```sh
$ cp -R junk /tmp
```

## 兼容性

历史版本的 `cp` 实用程序有 `-r` 选项。本实现支持该选项，但其行为与历史 FreeBSD 行为不同。强烈建议不要使用此选项，因为其行为依赖于实现。在 FreeBSD 中，`-r` 是 `-RL` 的同义词，除非被其他标志修改，否则工作方式相同。`-r` 的历史实现有所不同，它们在重建层次结构时将特殊文件作为普通文件复制。

`-a`、`-l`、`-N`、`-n`、`-s`、`-v` 和 `-x` 选项是非标准的，不建议在脚本中使用。

## 参见

[mv(1)](https://github.com/FreeBSD-Ask/freebsd-man-sc/blob/main/man1/mv.1.md), umask(2), fts(3), symlink(7)

## 标准

`cp` 命令预期与 IEEE Std 1003.2 ("POSIX.2") 兼容。

## 历史

`cp` 命令首次出现于 Version 1 AT\&T UNIX。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man1/cp.1.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
