首页·终端命令

cp

文件与目录

用法

cp -r [source] [dest]

复制文件或整个目录树;目标是否已存在,决定源是变成那个名字还是被放进它里面。

常用选项

选项含义
-rCopy directories and their contents
-aArchive: -r plus permissions, times and symlinks kept
-pKeep mode, owner and timestamps
-iAsk before overwriting
-nNever overwrite a file that already exists
-vName each file as it is copied
-uGNU: copy only when the source is newer

示例

cp -a site/ backup/

Tree copied with permissions and times intact.

cp config.yml config.yml.bak

Quick backup beside the original.

cp -r src/. dest/

Copies the contents of src into an existing dest.

移动、删除和查找。有些命令不会再问一遍,所以顺手加 -i 是在救自己。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. cp 是做什么的?

复制文件或整个目录树;目标是否已存在,决定源是变成那个名字还是被放进它里面。

Q. 怎么写?

cp -r [source] [dest] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 7 个,完整列表在 man cp。这条命令属于文件与目录。

相关命令

man 手册: man cp