描述:复制文件或目录spa
用法:cp [-adfilprsu] 源文件 目标文件code
cp [option] source1 source2 cource3.... directory递归
经常使用选项:io
-a:至关于-pdr test
-d:若源文件为链接文件,则复制链接文件属性而非文件自己;di
-f:为强制的意思,若目标文件已经存在且没法开启,则删除后再尝试一次;文件
-i:若目标文件已经存在,在覆盖时先询问;备份
-l:进行硬链接文件的建立,而非复制文件自己;co
-p:连同文件的属性一块儿复制过去,而非使用默认属性(备份经常使用);background
-r:递归持续复制
-s:复制成为符号连接文件,即快捷方式文件;
-u:若destination比source旧才更新
-v:显示指令执行过程
示例:1.复制整个目录
目标目录存在时:整个源目录比复制到目标目录里面。
[root@share19 ~]# cp -a test1 test2 [root@share19 ~]# ll total 4 -rw-------. 1 root root 1149 Jan 15 10:32 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 May 23 14:44 test1 drwxr-xr-x. 3 root root 18 May 23 14:46 test2 [root@share19 ~]# cd test2 [root@share19 test2]# ll total 0 drwxr-xr-x. 2 root root 6 May 23 14:44 test1 [root@share19 test2]#
目标目录不存在时:源目录与目标目录在一个路径下。
[root@share19 ~]# ll total 4 -rw-------. 1 root root 1149 Jan 15 10:32 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 May 23 14:44 test1 drwxr-xr-x. 4 root root 30 May 23 14:48 test2 [root@share19 ~]# cp -a test1 test3 [root@share19 ~]# ll total 4 -rw-------. 1 root root 1149 Jan 15 10:32 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 May 23 14:44 test1 drwxr-xr-x. 4 root root 30 May 23 14:48 test2 drwxr-xr-x. 2 root root 6 May 23 14:44 test3
2.复制的 log.log 创建一个连结档 log_link.log
[root@share19 ~]# cp -s log.log log_link.log [root@share19 ~]# ll total 1 lrwxrwxrwx. 1 root root 7 May 23 14:51 log_link.log -> log.log -rw-r--r--. 1 root root 0 May 23 14:51 log.log