computer_knowledge_notes/Software/文件管理/cp.md

40 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#### 语法
```bash
cp [OPTION] [-T] SOURCE DEST # 将文件SOURCE复制到文件DEST
cp [OPTION] SOURCE DIRECTORY # 将若干SOURCE复制到目录DIRECTORY
cp [OPTION] -t DIRECTORY SOURCE # 将若干SOURCE复制到目录DIRECTORY
```
#### 选项
```
-a, --archive # 等价于-dR --preserve=all其意义为递归的复制但不追踪符号链接且要保留所有的属性
-d # 等价于--no-dereference --preserve=links
-P, --no-dereference # 不追踪符号链接
--preserve[=ATTR_LIST] # 保留指定属性(默认:mode,ownership,timetamps),还有附加属性(context,links,xattr,all)
-R: 等价于 -r 或 --recursive递归地复制
-t DIRECTORY: 等价于 --target-directory=DIRECTORY将SOURCE复制到文件夹DIRECTORY。
-T 等价于 --no-target-directory 将DEST作为一般的文件。
```
#### 实例
- 多个文件复制到一个文件夹中
`cp -t dir a b c d #将a,b,c,d都复制到文件夹dir中`
- 在当前目录下创建文件夹dir1的副本dir2
`cp -r dir1 dir2 # dir2不能存在否则会把dir1复制到dir2目录下`
#### 出错处理
- 复制文件夹时出现`cp: omitting directory`的解决办法
这是由于文件夹包含子文件夹所致,使用`-r`选项即可