cp ¶
cp 命令常用案例 ¶
命令简介 ¶
cp 用于复制文件或目录。cp的英文全称是copy
三种使用语法 ¶
cp [选项]... [-T] 源文件 目标文件 # 单源复制
cp [选项]... 源文件... 目标目录 # 多源复制到目录
cp [选项]... -t 目标目录 源文件... # 先指定目标目录
选项说明 ¶
| 选项 | 说明 |
|---|---|
-a, --archive |
归档复制,等同于 -dR --preserve=all(保留所有属性) |
--attributes-only |
仅复制文件属性,不复制数据内容 |
--backup[=CONTROL] |
若目标文件已存在,先备份再覆盖 |
-b |
类似 --backup,但不接受参数 |
--copy-contents |
递归复制特殊文件的内容 |
-d |
等同于 --no-dereference --preserve=links |
-f, --force |
若目标无法打开,删除再重试(与 -n 同时使用时忽略) |
-i, --interactive |
覆盖前提示确认(覆盖 -n 选项) |
-H |
仅跟随源中命令行指定的符号链接 |
-l, --link |
创建硬链接而不是复制文件 |
-L, --dereference |
总是跟随源中的符号链接 |
-n, --no-clobber |
不覆盖已存在的文件(覆盖 -i 选项) |
-P, --no-dereference |
从不跟随源中的符号链接 |
-p |
等同于 --preserve=mode,ownership,timestamps |
--preserve[=属性列表] |
保留指定属性(默认:模式、所有者、时间戳);可选:context、links、xattr、all |
--no-preserve=属性列表 |
不保留指定属性 |
--parents |
在目标目录中保留完整的源路径 |
-R, -r, --recursive |
递归复制目录 |
--reflink[=WHEN] |
控制写时复制(CoW)拷贝,见下方说明 |
--remove-destination |
打开目标前先删除已存在的目标文件(与 --force 不同) |
--sparse=WHEN |
控制稀疏文件的创建,见下方说明 |
--strip-trailing-slashes |
移除源参数末尾的斜杠 |
-s, --symbolic-link |
创建符号链接而不是复制 |
-S, --suffix=后缀 |
覆盖默认的备份后缀(默认 ~) |
-t, --target-directory=目录 |
将源全部复制到指定目录 |
-T, --no-target-directory |
将目标视为普通文件(非目录) |
-u, --update |
仅当源文件比目标新或目标不存在时才复制 |
-v, --verbose |
显示正在执行的操作 |
-x, --one-file-system |
限制在当前文件系统内 |
-Z |
将目标文件的 SELinux 上下文设为默认类型 |
--context[=CTX] |
类似 -Z,或指定 SELinux/SMACK 上下文 |
--help |
显示帮助信息 |
--version |
显示版本信息 |
稀疏文件说明 ¶
--sparse=auto:默认,自动检测并创建稀疏文件--sparse=always:只要源包含足够长的零字节序列就创建稀疏文件--sparse=never:禁止创建稀疏文件
Reflink(写时复制)说明 ¶
--reflink=always:执行轻量级拷贝,数据块仅在修改时复制;不支持则失败--reflink=auto:优先尝试轻量级拷贝,不支持则回退到标准拷贝--reflink=never:确保执行标准拷贝
备份相关 ¶
- 默认备份后缀为
~,可通过--suffix或SIMPLE_BACKUP_SUFFIX环境变量修改 - 版本控制方式可通过
--backup或VERSION_CONTROL环境变量设置:none, off:不备份numbered, t:编号备份(如file~1~)existing, nil:已有编号则编号备份,否则简单备份simple, never:始终简单备份(如file~)
常用案例 ¶
1. 复制文件到目录 ¶
cp file.txt /tmp/
2. 复制文件并重命名 ¶
cp file.txt file_backup.txt
3. 复制多个文件到目录 ¶
cp file1.txt file2.txt file3.txt /backup/
4. 递归复制整个目录 ¶
cp -r /source/dir /dest/dir
5. 归档复制(保留所有属性) ¶
cp -a /source/dir /dest/dir
常用于备份,保留权限、所有者、时间戳等。
6. 覆盖前提示确认 ¶
cp -i file.txt /target/
# 输出:overwrite '/target/file.txt'? y/n
7. 不覆盖已存在的文件 ¶
cp -n file.txt /target/
8. 仅复制更新的文件(增量复制) ¶
cp -u source.txt dest.txt
# 仅当 source.txt 比 dest.txt 新,或 dest.txt 不存在时才复制
9. 创建硬链接(而非复制) ¶
cp -l file.txt hardlink.txt
10. 创建符号链接 ¶
cp -s file.txt symlink.txt
11. 显示复制过程 ¶
cp -v file.txt /tmp/
# 输出:'file.txt' -> '/tmp/file.txt'
12. 复制时保留完整路径 ¶
cp --parents /path/to/source/file.txt /backup/
# 在 /backup/ 下创建 /path/to/source/file.txt
13. 复制时先备份目标文件 ¶
cp -b file.txt /target/
# 若 /target/file.txt 存在,先备份为 file.txt~
14. 使用编号备份 ¶
cp --backup=numbered file.txt /target/
# 备份为 file.txt.~1~, file.txt.~2~ 等b
15. 仅复制文件属性(不复制内容) ¶
cp --attributes-only file.txt newfile.txt
# 创建空文件,但保留原文件的权限和时间戳
16. 所有源复制到指定目录(使用 -t) ¶
cp -t /target/dir file1.txt file2.txt file3.txt
17. 强制复制(删除已存在目标后再尝试) ¶
cp -f file.txt /target/
18. 递归复制但不跨越文件系统 ¶
cp -rx /source/ /dest/
19. 使用写时复制(CoW,适用于 Btrfs/XFS 等) ¶
cp --reflink=always largefile.iso clone.iso
# 快速复制大文件,仅在实际修改时才占用额外空间