cp 命令
复制(copy)文件或目录
语法
cp (选项)(参数1)(参数2)
选项
-r 递归复制整个目录树
-p 保持源文件属性不变复制
-a 归档复制文件或目录树并保持属性不变(多用于备份)
-l 对源文件建立硬连接,而非复制
-s 对源文件建立符号连接,而非复制
-v 显示执行过程
参数
参数1需复制文件或目录,参数2目录文件或目录;
实例
复制根目录下的itkaifang文件到root家目录下并重新命名为it
[root@itkaifang ~]# ls / a b boot dev f home lib lost+found lv-D misc net proc sbin soft sys tmp usr a.txt bin c etc haha itkaifang lib64 lv-C media mnt opt root selinux srv tftpboot u01 var [root@itkaifang ~]# cp /itkaifang /root/it [root@itkaifang ~]# ls anaconda-ks.cfg Desktop install.log install.log.syslog it
复制根下a目录到root家目录为b目录
[root@itkaifang ~]# cp /a /root/a cp: omitting directory `/a' [root@itkaifang ~]# cp -r /a /root/b [root@itkaifang ~]# ll total 68 -rw------- 1 root root 1146 Feb 21 20:34 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 Feb 26 19:03 b drwxr-xr-x 2 root root 4096 Feb 25 19:14 Desktop -rw-r--r-- 1 root root 38449 Feb 23 20:12 install.log -rw-r--r-- 1 root root 3686 Feb 21 20:34 install.log.syslog -rw-r--r-- 1 root root 29 Feb 26 19:00 it
复制根下a.txt到root家目录下it2并保持属性不变
[root@itkaifang ~]# ls -l /a.txt -rw-r--r-- 1 root root 29 Feb 26 16:12 /a.txt [root@itkaifang ~]# cp -p /a.txt /root/it2 [root@itkaifang ~]# ll total 72 -rw------- 1 root root 1146 Feb 21 20:34 anaconda-ks.cfg drwxr-xr-x 3 root root 4096 Feb 26 14:44 c drwxr-xr-x 2 root root 4096 Feb 25 19:14 Desktop -rw-r--r-- 1 root root 38449 Feb 23 20:12 install.log -rw-r--r-- 1 root root 3686 Feb 21 20:34 install.log.syslog -rw-r--r-- 1 root root 29 Feb 26 16:53 it -rw-r--r-- 1 root root 29 Feb 26 16:12 it2 [root@itkaifang ~]# date Sun Feb 26 19:14:04 CST 2017
复制根下a目录到root家目录下d目录并保持属性不变
[root@itkaifang ~]# ls -l /a total 0 -rw-r--r-- 1 root root 0 Feb 26 15:15 b [root@itkaifang ~]# date Sun Feb 26 19:21:32 CST 2017 [root@itkaifang ~]# cp -a /a /root/d [root@itkaifang ~]# ll total 72 -rw------- 1 root root 1146 Feb 21 20:34 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 Feb 26 15:15 d drwxr-xr-x 2 root root 4096 Feb 25 19:14 Desktop -rw-r--r-- 1 root root 38449 Feb 23 20:12 install.log -rw-r--r-- 1 root root 3686 Feb 21 20:34 install.log.syslog -rw-r--r-- 1 root root 29 Feb 26 16:53 it -rw-r--r-- 1 root root 29 Feb 26 16:12 it2
注意:cp -a 在用于备份时不可重复对一个文件或目录备份在同一目录下,否则会覆盖复制并修改其时间戳
[root@itkaifang ~]# ls -l /a total 0 -rw-r--r-- 1 root root 0 Feb 26 15:15 b [root@itkaifang ~]# date Sun Feb 26 19:21:32 CST 2017 [root@itkaifang ~]# cp -a /a /root/d [root@itkaifang ~]# ll total 72 -rw------- 1 root root 1146 Feb 21 20:34 anaconda-ks.cfg drwxr-xr-x 2 root root 4096 Feb 26 15:15 d drwxr-xr-x 2 root root 4096 Feb 25 19:14 Desktop -rw-r--r-- 1 root root 38449 Feb 23 20:12 install.log -rw-r--r-- 1 root root 3686 Feb 21 20:34 install.log.syslog -rw-r--r-- 1 root root 29 Feb 26 16:53 it -rw-r--r-- 1 root root 29 Feb 26 16:12 it2 [root@itkaifang ~]# cp -a /a /root/d [root@itkaifang ~]# stat d File: `d' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 11709947 Links: 3 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-02-26 15:15:11.000000000 +0800 Modify: 2017-02-26 19:26:01.000000000 +0800 Change: 2017-02-26 19:26:01.000000000 +0800 [root@itkaifang ~]# stat /a File: `/a' Size: 4096 Blocks: 8 IO Block: 4096 directory Device: 803h/2051d Inode: 3957185 Links: 2 Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2017-02-26 15:15:11.000000000 +0800 Modify: 2017-02-26 15:15:09.000000000 +0800 Change: 2017-02-26 15:15:09.000000000 +0800