Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

3.12. cp - copy files and directories

3.12.1. copy directories recursively

cp -r /etc/* ~/myetc
			

3.12.2. 覆盖已存在的文件

overwrite an existing file

-f, --force 覆盖已经存在的目标文件而不给出提示。 if an existing destination file cannot be opened, remove it and try again (this option is ignored when the -n option is also used)

当使用 -f 参数时仍然会提示询问覆盖

			
cp -f file1 file2

cp: overwrite 'file2'?			
			
			

使用 alias 命令查看,可以看到 cp 命令 增加 -i 参数,使用 unalias cp 可以删除 cp 别名。

			
# alias cp
alias cp='cp -i'

# unalias cp
# alias cp
-bash: alias: cp: not found
			
			

另一种解决方案是在 cp 前增加斜杠禁止别名

			
\cp -f file1 file2			
			
			

3.12.3. -a, --archive same as -dR --preserve=all

# cp -a file file2
			

-a 参数可以保留原文件的日期与权限等等信息。

# ll
-rw-r--r--. 1 root root      2559 Aug 27 05:00 yum.sh

# cp -a yum.sh yum1.sh
# cp yum.sh yum2.sh

# ll yum*
-rw-r--r--. 1 root root 2559 Aug 27 05:00 yum1.sh
-rw-r--r--. 1 root root 2559 Aug 27 05:58 yum2.sh
-rw-r--r--. 1 root root 2559 Aug 27 05:00 yum.sh
			

现在可以看到 yum1.sh 与 yum.sh 日期是相同的,而没有实现-a参数的 yum2.sh 日期为当前日期。