cp


功能说明:

复制文件或目录

语法:

cp [-adfilprsu] 源文件(source目标文件(destination

cp [optins] source1 source2 source3 ... Directory

参数:

-a :相当于-pdr的意思

-d :若源文件为连接文件的属性(linkfile),则复制连接文件属性而非文件本身

-f :强制(force)的意思,若目标文件已经存在且无法开启,则删除后再尝试一次

-i :若目标文件已经存在,在覆盖时会先询问操作的进行

-l :进行硬连接(hard link)的连接文件创建,而非复制文件本身

-p :连同文件的属性一起复制过去,而非使用默认属性备份常用

-r :递归持续复制,用于目录的复制行为

-s :复制成为符号连接文件(syrnbolic.link),即快捷方式文件

-u :若destinationsource旧才更新destination


如果源文件有一个以上,则最后一个目的文件一定是目录



命令实践:

[root@yubinghost ~]# touch yubing.txt

[root@yubinghost ~]# cp yubing.txt /tmp/     复制到/tmp目录下

[root@yubinghost ~]# cd /tmp

[root@yubinghost tmp]# ll

total 20

drwx------ 2 root root 4096 Apr 11 13:42 ssh-PApiVj3092

drwxr-xr-x 2 root root 4096 Apr  7 12:49 test

drwxr-xr-x 3 root root 4096 Apr  7 12:49 test1

drwxr--r-- 2 root root 4096 Apr  7 12:50 test2

-rw-r--r-- 1 root root    0 Apr  5 14:54 test.txt

-rw-r--r-- 1 root root    0 Apr 11 15:21 yubing.txt

[root@yubinghost tmp]# cd

[root@yubinghost ~]# ll

total 8476

-rw------- 1 root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1 root root 3422452 Apr  8 10:57 etc.tar.bz2

-rw-r--r-- 1 root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1 root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1 root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1 root root      18 Apr  5 14:43 o1

-rw-r--r-- 1 root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1 root root      18 Apr  5 14:47 test.txt

-rw-r--r-- 1 root root       0 Apr 11 15:21 yubing.txt

[root@yubinghost ~]# cp yubing.txt /tmp/    只有覆盖的时候才会提示

cp: overwrite `/tmp/yubing.txt'? y

[root@yubinghost ~]#

[root@yubinghost ~]# ll

total 8476

-rw------- 1 root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1 root root 3422452 Apr  8 10:57 etc.tar.bz2

-rw-r--r-- 1 root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1 root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1 root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1 root root      18 Apr  5 14:43 o1

-rw-r--r-- 1 root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1 root root      18 Apr  5 14:47 test.txt

-rw-r--r-- 1 root root       0 Apr 11 15:21 yubing.txt

[root@yubinghost ~]# umask     查看系统默认权限

0022

[root@yubinghost ~]# chmod 777 yubing.txt     设置文件权限

[root@yubinghost ~]# ll

total 8476

-rw------- 1 root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1 root root 3422452 Apr  8 10:57 etc.tar.bz2

-rw-r--r-- 1 root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1 root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1 root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1 root root      18 Apr  5 14:43 o1

-rw-r--r-- 1 root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1 root root      18 Apr  5 14:47 test.txt

-rwxrwxrwx 1 root root       0 Apr 11 15:21 yubing.txt    文件权限已经更改成777

[root@yubinghost ~]# cp yubing.txt /tmp/  

[root@yubinghost ~]# cd /tmp

[root@yubinghost tmp]# ll

total 20

drwx------ 2 root root 4096 Apr 11 13:42 ssh-PApiVj3092

drwxr-xr-x 2 root root 4096 Apr  7 12:49 test

drwxr-xr-x 3 root root 4096 Apr  7 12:49 test1

drwxr--r-- 2 root root 4096 Apr  7 12:50 test2

-rw-r--r-- 1 root root    0 Apr  5 14:54 test.txt

-rwxr-xr-x1 root root    0 Apr 11 15:26 yubing.txt 复制后发现文件权限没有变更

[root@yubinghost tmp]# rm yubing.txt

rm: remove regular empty file `yubing.txt'? y

[root@yubinghost tmp]# cd

[root@yubinghost ~]# cp -p yubing.txt /tmp/   加上-p参数

[root@yubinghost ~]# cd /tmp

[root@yubinghost tmp]# ll

total 20

drwx------ 2 root root 4096 Apr 11 13:42 ssh-PApiVj3092

drwxr-xr-x 2 root root 4096 Apr  7 12:49 test

drwxr-xr-x 3 root root 4096 Apr  7 12:49 test1

drwxr--r-- 2 root root 4096 Apr  7 12:50 test2

-rw-r--r-- 1 root root    0 Apr  5 14:54 test.txt

-rwxrwxrwx 1 root root    0 Apr 11 15:21 yubing.txt  直接将目标文件的权限也复制过来了

[root@yubinghost ~]# ln -s yubing.txt yubing.link   创建符号连接

[root@yubinghost ~]# ll

total 8476

-rw------- 1 root root     939 Apr  3 12:38 anaconda-ks.cfg

-rw-r--r-- 1 root root 3422452 Apr  8 10:57 etc.tar.bz2

-rw-r--r-- 1 root root 5172939 Apr  8 10:55 etc.tar.gz

-rw-r--r-- 1 root root   23947 Apr  3 12:37 install.log

-rw-r--r-- 1 root root    3619 Apr  3 12:37 install.log.syslog

-rw-r--r-- 1 root root      18 Apr  5 14:43 o1

-rw-r--r-- 1 root root       0 Apr  5 14:36 oldboy.txt

-rw-r--r-- 1 root root      18 Apr  5 14:47 test.txt

lrwxrwxrwx 1 root root      10 Apr 11 15:41 yubing.link -> yubing.txt

-rwxrwxrwx 1 root root       0 Apr 11 15:21 yubing.txt

\[root@yubinghost ~]# cp yubing.link /tmp

[root@yubinghost ~]# cd /tmp && ll

total 20

drwx------ 2 root root 4096 Apr 11 13:42 ssh-PApiVj3092

drwxr-xr-x 2 root root 4096 Apr  7 12:49 test

drwxr-xr-x 3 root root 4096 Apr  7 12:49 test1

drwxr--r-- 2 root root 4096 Apr  7 12:50 test2

-rw-r--r-- 1 root root    0 Apr  5 14:54 test.txt

-rwxr-xr-x 1 root root    0 Apr 11 15:42 yubing.link   复制过来的是文件本身,而不是符号连接

-rwxrwxrwx 1 root root    0 Apr 11 15:21 yubing.txt                        

[root@yubinghost tmp]# cd

[root@yubinghost ~]# cp -d yubing.link /tmp/   加上-d参数

cp: overwrite `/tmp/yubing.link'? y

[root@yubinghost ~]# cd /tmp ;ll

total 20

drwx------ 2 root root 4096 Apr 11 13:42 ssh-PApiVj3092

drwxr-xr-x 2 root root 4096 Apr  7 12:49 test

drwxr-xr-x 3 root root 4096 Apr  7 12:49 test1

drwxr--r-- 2 root root 4096 Apr  7 12:50 test2

-rw-r--r-- 1 root root    0 Apr  5 14:54 test.txt

lrwxrwxrwx 1 root root   10 Apr 11 15:43 yubing.link -> yubing.txt 复制过来的是连接文件属性

-rwxrwxrwx 1 root root    0 Apr 11 15:21 yubing.txt                    

[root@yubinghost tmp]#