简介
cp 命令,主要用来复制文件和目录,同时借助某些选项,还可以实现复制整个目录,以及比对两文件的新旧而予以升级等功能。
命令格式
cp [OPTION]… [-T] SOURCE DEST cp [OPTION]… SOURCE… DIRECTORY cp [OPTION]… -t DIRECTORY SOURCE…
常用参数
| -r | 递归复制(若给出的源文件是一个目录文件,复制该目录下所有的子目录和文件)。 |
|---|---|
| -i | 如果目标文件已经存在,则会询问是否覆盖 |
| -f | 覆盖已经存在的目标文件而不给出提示 |
| -l | 把目标文件建立为源文件的硬链接文件,而不是复制源文件 |
| -s | 把目标文件建立为源文件的软链接文件,而不是复制源文件 |
| -d | 如果源文件为软链接(对硬链接无效),则复制出的目标文件也为软链接 |
| -p | 复制后目标文件保留源文件的属性(包括所有者、所属组、权限和时间 |
| -a | 相当于 -dpr 选项的集合,通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容 |
复制的几种场景
cp SRC DESTSRC是文件 :DEST不存在新建 DEST 并将 SRC 中的内容填充到 DEST 中。DEST存在DEST为文件 :将 SRC 中的内容覆盖到 DEST 中。建议使用 -i 选项DEST为目录 :在 DEST 下新建与原文件同名的文件,并将 SRC 的内容填充到新文件中。cp SRC... DESTSRC... 多个文件DEST必须存在,且为目录。其他情况均会出错。cp SRC DESTSRC是目录 :此时使用选项 -rDEST不存在创建指定目录,复制 SRC 目录中所有的文件到 DEST 中。DEST存在DEST是文件 : 报错DEST是目录 : 复制 SRC 文件夹到 DEST 中。
## 创建3个复制用的文件[root@localhost ~]# touch file_{a,b,c}.txt[root@localhost ~]# lsfile_a.txt file_b.txt file_c.txt[root@localhost ~]# echo aaa > file_a.txt[root@localhost ~]# cat file_a.txtaaa## 复制 file_a.txt 到一个并不存在的文件中[root@localhost ~]# cp file_a.txt a[root@localhost ~]# lltotal 0-rw-r--r--. 1 root root 0 May 13 01:01 a-rw-r--r--. 1 root root 0 May 13 01:01 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:01 file_b.txt-rw-r--r--. 1 root root 0 May 13 01:01 file_c.txt[root@localhost ~]# cat aaaa## 复制文件到一个已经存在的文件中[root@localhost ~]# echo bbb > b[root@localhost ~]# cat bbbb[root@localhost ~]# cp file_a.txt bcp: overwrite ‘b’? y ## 因为已经存在,所以会询问是否覆盖,后续会讲解强制覆盖[root@localhost ~]# cat baaa## 复制文件到一个目录中[root@localhost ~]# mkdir dir_a[root@localhost ~]# cp file_a.txt dir_a[root@localhost ~]# ll dir_atotal 4-rw-r--r--. 1 root root 4 May 13 01:08 file_a.txt## 复制多个文件,那么 test 必须是一个目录[root@localhost ~]# cp file_a.txt file_b.txt file_c.txt testcp: target ‘test’ is not a directory ## test 不存在,所以报错[root@localhost ~]# mkdir test[root@localhost ~]# cp file_a.txt file_b.txt file_c.txt test[root@localhost ~]# ll testtotal 0-rw-r--r--. 1 root root 0 May 13 01:02 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:02 file_b.txt-rw-r--r--. 1 root root 0 May 13 01:02 file_c.txt## 复制目录到一个不存在的目录[root@localhost ~]# ll -d testdrwxr-xr-x. 2 root root 57 May 13 01:02 test[root@localhost ~]# ll testtotal 0-rw-r--r--. 1 root root 0 May 13 01:02 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:02 file_b.txt-rw-r--r--. 1 root root 0 May 13 01:02 file_c.txt[root@localhost ~]# cp test test2 ## 如果直接复制会报错,因为test是一个目录,所以需要使用 -r 选项cp: omitting directory ‘test’[root@localhost ~]# cp -r test test2[root@localhost ~]# ll test2total 0-rw-r--r--. 1 root root 0 May 13 01:10 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:10 file_b.txt-rw-r--r--. 1 root root 0 May 13 01:10 file_c.txt## 复制文件到一个已经存在的目录[root@localhost ~]# mkdir test3[root@localhost ~]# cp -r test test3[root@localhost ~]# ll -R test3test3:total 0drwxr-xr-x. 2 root root 57 May 13 01:12 testtest3/test:total 0-rw-r--r--. 1 root root 0 May 13 01:12 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:12 file_b.txt-rw-r--r--. 1 root root 0 May 13 01:12 file_c.txt
示例
[root@localhost ~]# touch file_{a,b}.txt[root@localhost ~]# lltotal 0-rw-r--r--. 1 root root 0 May 13 01:17 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:17 file_b.txt## -i 如果目标文件已经存在,则会询问是否覆盖[root@localhost ~]# cp file_a.txt file_b.txt[root@localhost ~]# cp -i file_a.txt file_b.txtcp: overwrite ‘file_b.txt’? y## -f:覆盖已经存在的目标文件而不给出提示。(默认就是 -f 不询问,强制覆盖)[root@localhost ~]# cp -f file_a.txt file_b.txt## -l:把目标文件建立为源文件的硬链接文件,而不是复制源文件;[root@localhost ~]# lltotal 4-rw-r--r--. 1 root root 3 May 13 01:22 file_a.txt-rw-r--r--. 1 root root 0 May 13 01:22 file_b.txt[root@localhost ~]# cp -l file_a.txt hard_file_a.txt[root@localhost ~]# ll -i *file_a.txt68216865 -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt68216865 -rw-r--r--. 2 root root 3 May 13 01:22 hard_file_a.txt## -s:把目标文件建立为源文件的软链接文件,而不是复制源文件;[root@localhost ~]# cp -s file_a.txt soft_file_a.txt68216865 -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt68216865 -rw-r--r--. 2 root root 3 May 13 01:22 hard_file_a.txt68216870 lrwxrwxrwx. 1 root root 10 May 13 01:31 soft_file_a.txt -> file_a.txt## -d:如果源文件为软链接(对硬链接无效),则复制出的目标文件也为软链接;## 如果在复制软链接文件时不使用 "-d" 选项,则 cp 命令复制的是源文件,而不是软链接文件;## 只有加入了 "-d" 选项,才会复制软链接文件。[root@localhost ~]# ll -i *file_a.txt68216865 -rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt68216865 -rw-r--r--. 2 root root 3 May 13 01:22 hard_file_a.txt68216870 lrwxrwxrwx. 1 root root 10 May 13 01:31 soft_file_a.txt -> file_a.txt[root@localhost ~]# cp soft_file_a.txt soft_1_file_a.txt[root@localhost ~]# cp -d soft_file_a.txt soft_2_file_a.txt[root@localhost ~]# ll -i soft_*file_a.txt68216872 -rw-r--r--. 1 root root 3 May 13 01:37 soft_1_file_a.txt ## 实际复制的是软链接的源文件,而不是软链接文件68216871 lrwxrwxrwx. 1 root root 10 May 13 01:35 soft_2_file_a.txt -> file_a.txt68216870 lrwxrwxrwx. 1 root root 10 May 13 01:31 soft_file_a.txt -> file_a.txt## -p:复制后目标文件保留源文件的属性(包括所有者、所属组、权限和时间);[root@localhost ~]# ll file_a.txt-rw-r--r--. 2 root root 3 May 13 01:22 file_a.txt[root@localhost ~]# chown gkdaxue file_a.txt[root@localhost ~]# ll file_a.txt-rw-r--r--. 2 gkdaxue root 3 May 13 01:22 file_a.txt[root@localhost ~]# cp file_a.txt root_file_a.txt[root@localhost ~]# ll file_a.txt root_file_a.txt-rw-r--r--. 2 gkdaxue root 3 May 13 01:22 file_a.txt-rw-r--r--. 1 root root 3 May 13 01:42 root_file_a.txt ## 文件的属主和时间都发生了变化[root@localhost ~]# cp -p file_a.txt gkdaxue_file_a.txt[root@localhost ~]# ll file_a.txt root_file_a.txt gkdaxue_file_a.txt-rw-r--r--. 2 gkdaxue root 3 May 13 01:22 file_a.txt-rw-r--r--. 1 gkdaxue root 3 May 13 01:22 gkdaxue_file_a.txt ## 文件的属主和时间都被复制了下来-rw-r--r--. 1 root root 3 May 13 01:42 root_file_a.txt
