cp
是 Linux 和类 Unix 操作系统中的一个基本命令,用于复制文件或目录。它是 "copy" 的缩写,支持将文件复制到指定位置,或者递归复制整个目录。
以下是对 cp
命令的详细介绍,包括基本用法、常用选项和示例。
基本语法
- 源文件/目录:要复制的文件或目录。
- 目标文件/目录:复制后的文件或目录路径。
- 选项:用于定制复制行为,例如递归复制、保留属性、显示详细信息等。
常用选项
选项 |
功能 |
-r 或 -R |
递归复制目录及其内容(必须用于复制目录)。 |
-i |
如果目标路径存在,提示用户确认是否覆盖文件。 |
-f |
强制复制,覆盖目标文件而不提示(默认行为)。 |
-u |
仅在源文件比目标文件新或目标文件不存在时复制。 |
-v |
显示复制过程的详细信息(verbose)。 |
-p |
保留文件的属性(如权限、时间戳、所有者等)。 |
-a |
归档模式,相当于 -dR --preserve=all ,保留文件的所有属性并递归复制。 |
--backup |
在覆盖文件之前创建备份文件。 |
--preserve |
指定保留文件属性(如 mode 、ownership 、timestamps )。 |
--parents |
保留源文件的目录结构。 |
详解选项
递归复制目录(-r
或 -R
)
- 默认情况下,
cp
不能直接复制目录。使用 -r
或 -R
选项,可以递归复制目录及其内容。
- 示例:
1
| cp -r source_dir target_dir
|
- 复制整个目录
source_dir
到 target_dir
。
提示覆盖(-i
)
- 如果目标文件已存在,
cp
默认直接覆盖文件。使用 -i
选项,复制前会提示用户确认。
- 示例:
1
| cp -i file1.txt /path/to/destination/
|
- 如果目标路径中已存在
file1.txt
,会提示:1
| overwrite '/path/to/destination/file1.txt'? (y/n)
|
强制复制(-f
)
- 强制覆盖目标文件,不会提示用户确认(默认行为)。
- 示例:
1
| cp -f file1.txt /path/to/destination/
|
仅复制更新的文件(-u
)
- 如果目标文件不存在,或者源文件比目标文件新,则复制文件;否则跳过。
- 示例:
1
| cp -u file1.txt /path/to/destination/
|
显示详细信息(-v
)
- 显示每个文件的复制过程。
- 示例:
1
| cp -v file1.txt /path/to/destination/
|
输出示例:1
| 'file1.txt' -> '/path/to/destination/file1.txt'
|
保留文件属性(-p
)
- 复制时保留文件的权限、时间戳和所有者信息。
- 示例:
1
| cp -p file1.txt /path/to/destination/
|
归档模式(-a
)
-a
等价于 -dR --preserve=all
,递归复制目录,并保留文件的所有属性。
- 示例:
1
| cp -a source_dir target_dir
|
- 复制
source_dir
到 target_dir
,保留所有文件属性。
保留目录结构(--parents
)
- 复制文件时保留源文件的目录层次结构。
- 示例:
1
| cp --parents source_dir/file1.txt /path/to/destination/
|
- 如果
file1.txt
位于 source_dir/sub_dir/
中,目标路径会创建 /path/to/destination/source_dir/sub_dir/file1.txt
。
示例
复制单个文件
1
| cp file1.txt /path/to/destination/
|
- 将
file1.txt
复制到目标目录 /path/to/destination/
。
复制多个文件到目标目录
1
| cp file1.txt file2.txt /path/to/destination/
|
- 将
file1.txt
和 file2.txt
复制到 /path/to/destination/
。
递归复制目录
1
| cp -r source_dir /path/to/destination/
|
- 将整个目录
source_dir
及其内容复制到 /path/to/destination/
。
覆盖目标文件前提示确认
1
| cp -i file1.txt /path/to/destination/
|
- 如果目标路径中存在同名文件,会提示用户确认是否覆盖。
仅复制更新的文件
1
| cp -u file1.txt /path/to/destination/
|
- 如果
file1.txt
比目标路径中的同名文件新,则复制;否则不复制。
显示复制过程的详细信息**
1
| cp -v file1.txt /path/to/destination/
|
输出示例:
1
| 'file1.txt' -> '/path/to/destination/file1.txt'
|
复制并保留文件属性
1
| cp -p file1.txt /path/to/destination/
|
复制目录并保留所有属性
1
| cp -a source_dir /path/to/destination/
|
- 将
source_dir
及其内容复制到 /path/to/destination/
,并保留所有文件属性。
保留目录结构复制文件
1
| cp --parents source_dir/file1.txt /path/to/destination/
|
- 在目标路径中保留
source_dir
的目录结构。
常见错误及解决方法
- 权限不足
1
| cp: cannot create regular file '/path/to/destination/file1.txt': Permission denied
|
- 解决方法:使用
sudo
提权:1
| sudo cp file1.txt /path/to/destination/
|
- 文件不存在
1
| cp: cannot stat 'file1.txt': No such file or directory
|
- 目标目录不存在
1
| cp: cannot create directory '/path/to/destination/': No such file or directory
|
- 解决方法:先创建目标目录:
1 2
| mkdir -p /path/to/destination/ cp file1.txt /path/to/destination/
|
结合其他命令
配合 find
按条件复制文件
- 复制所有
.txt
文件到目标目录:1
| find /source_dir -name "*.txt" -exec cp {} /path/to/destination/ \;
|
配合通配符复制文件
- 复制当前目录下所有
.log
文件:1
| cp *.log /path/to/destination/
|
总结
cp
是 Linux 中常用的文件和目录复制命令,支持单个文件的复制、多文件的批量复制,以及目录递归复制。
- 通过选项(如
-r
、-i
、-p
、-a
等),可以灵活实现递归复制、保留属性、交互确认等功能。
- 谨慎使用覆盖选项(
-f
和 -i
),避免误操作导致重要文件丢失。