1.命令功能
cp :复制文件或目录,文件存在的会询问是否覆盖
2.命令格式
cp [选项] 文件或目录
3.命令参数
-i :覆盖时进行询问
-r :复制目录,递归复制
-f :强制复制
-p :保持文件属性并复制
--backup :为存在的文件进行备份
-a :相当于-pr
4.命令实例
例子1:交互式复制
[[email protected] ~]# cp /etc/inittab .
[[email protected] ~]# ls
anaconda-ks.cfg Desktop file1 file2 gg inittab install.log install.log.syslog
[[email protected] ~]# cp -i /etc/inittab .
cp: overwrite `./inittab‘? yes
例子2:复制目录,递归复制
[[email protected] ~]# cp -r /etc/ .
[[email protected] ~]# ls
anaconda-ks.cfg etc file2 inittab install.log.syslog
Desktop file1 gg install.log
例子3:强制复制
[[email protected] ~]# \cp -f /etc/inittab .
[[email protected] ~]# cp -i /etc/inittab .
cp: overwrite `./inittab‘? yes
[[email protected] ~]# \cp -f /etc/inittab . #因为cp是一个别名,使用反斜杠可以使用命令本身
[[email protected] ~]#
例子4:保持文件属性并复制
[[email protected] ~]# ll
total 72
-rw------- 1 root root 1259 Nov 17 19:18 anaconda-ks.cfg
drwx------ 2 root root 4096 Nov 17 19:25 Desktop
-rw-r--r-- 1 root root 0 Dec 21 17:07 file1
-rw-r--r-- 1 root root 0 Dec 21 17:12 file2
drwxr-xr-x 2 root root 4096 Dec 21 16:43 gg
-rw-r--r-- 1 root root 39302 Nov 17 19:17 install.log
-rw-r--r-- 1 root root 4062 Nov 17 19:17 install.log.syslog
-r-------- 1 xiaomo root 976 Nov 17 19:17 shadow
[[email protected] ~]# cp shadow /tmp/
[[email protected] ~]# ll /tmp/
total 16
drwx------ 2 root root 4096 Nov 17 19:45 ksocket-root
drwx------ 2 root root 4096 Nov 17 19:45 orbit-root
-r-------- 1 root root 976 Dec 21 18:18 shadow
drwx------ 2 root root 4096 Nov 17 19:44 ssh-RTxmSP5661
[[email protected] ~]# cp -p shadow /tmp/
cp: overwrite `/tmp/shadow‘? yes
[[email protected] ~]# ll /tmp/
total 16
drwx------ 2 root root 4096 Nov 17 19:45 ksocket-root
drwx------ 2 root root 4096 Nov 17 19:45 orbit-root
-r-------- 1 xiaomo root 976 Nov 17 19:17 shadow
drwx------ 2 root root 4096 Nov 17 19:44 ssh-RTxmSP5661
例子5:为存在的文件进行备份
[[email protected] ~]# cp -p --backup shadow /tmp/
cp: overwrite `/tmp/shadow‘? yes
[[email protected] ~]# ll /tmp/
total 20
drwx------ 2 root root 4096 Nov 17 19:45 ksocket-root
drwx------ 2 root root 4096 Nov 17 19:45 orbit-root
-r-------- 1 xiaomo root 976 Nov 17 19:17 shadow
-r-------- 1 xiaomo root 976 Nov 17 19:17 shadow~
drwx------ 2 root root 4096 Nov 17 19:44 ssh-RTxmSP5661
例子6:相当于-pr
[[email protected] ~]# ll
total 72
-rw------- 1 root root 1259 Nov 17 19:18 anaconda-ks.cfg
drwx------ 2 root root 4096 Nov 17 19:25 Desktop
-rw-r--r-- 1 root root 0 Dec 21 17:07 file1
-rw-r--r-- 1 root root 0 Dec 21 17:12 file2
drwxr-xr-x 2 root root 4096 Dec 21 16:43 gg
-rw-r--r-- 1 root root 39302 Nov 17 19:17 install.log
-rw-r--r-- 1 root root 4062 Nov 17 19:17 install.log.syslog
-r-------- 1 xiaomo root 976 Nov 17 19:17 shadow
[[email protected] ~]# cp -a shadow /tmp/
[[email protected] ~]# ll /tmp/
total 16
drwx------ 2 root root 4096 Nov 17 19:45 ksocket-root
drwx------ 2 root root 4096 Nov 17 19:45 orbit-root
-r-------- 1 xiaomo root 976 Nov 17 19:17 shadow
drwx------ 2 root root 4096 Nov 17 19:44 ssh-RTxmSP5661