Linux命令-cp命令

cp命令:
    将源文件复制至目标文件,或将多个源文件复制至目标目录。
    cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一。一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。
    命令参数:
-a, --archive    等于-dR --preserve=all

    --backup[=CONTROL    为每个已存在的目标文件创建备份

-b                类似--backup 但不接受参数

   --copy-contents        在递归处理是复制特殊文件内容

-d                等于--no-dereference --preserve=links

-f, --force        如果目标文件无法打开则将其移除并重试(当 -n 选项

                    存在时则不需再选此项)

-i, --interactive        覆盖前询问(使前面的 -n 选项失效)

-H                跟随源文件中的命令行符号链接

-l, --link            链接文件而不复制

-L, --dereference   总是跟随符号链接

-n, --no-clobber   不要覆盖已存在的文件(使前面的 -i 选项失效)

-P, --no-dereference   不跟随源文件中的符号链接

-p                等于--preserve=模式,所有权,时间戳

    --preserve[=属性列表   保持指定的属性(默认:模式,所有权,时间戳),如果

               可能保持附加属性:环境、链接、xattr 等

-R, -r, --recursive  复制目录及目录内的所有项目
------------------------------------------------------------------------------------------------
1.复制单个文件到目标目录
# ll test4
总用量 0
-rw-r--r-- 1 root root 0 1月  21 10:24 2020.txt
-rw-r--r-- 1 root root 0 2月  11 2021 2021.log
-rw-r--r-- 1 root root 0 1月  15 17:00 21.java
-rw-r--r-- 1 root root 0 1月  21 10:24 31.log
-rw-r--r-- 1 root root 0 1月  15 17:00 32.log
[root@root test01]# cp a1.txt test4
[root@root test01]# ll test4
总用量 4
-rw-r--r-- 1 root root   0 1月  21 10:24 2020.txt
-rw-r--r-- 1 root root   0 2月  11 2021 2021.log
-rw-r--r-- 1 root root   0 1月  15 17:00 21.java
-rw-r--r-- 1 root root   0 1月  21 10:24 31.log
-rw-r--r-- 1 root root   0 1月  15 17:00 32.log
-rw-r--r-- 1 root root 317 1月  21 14:59 a1.txt
------------------------------------------------------------------------------------------------
2.复制整个目录:
目标目录存在时:(整个源目录被复制到目标目录里面)
# ll test4
总用量 4
-rw-r--r-- 1 root root   0 1月  21 10:24 2020.txt
-rw-r--r-- 1 root root   0 2月  11 2021 2021.log
-rw-r--r-- 1 root root   0 1月  15 17:00 21.java
-rw-r--r-- 1 root root   0 1月  21 10:24 31.log
-rw-r--r-- 1 root root   0 1月  15 17:00 32.log
-rw-r--r-- 1 root root 317 1月  21 15:11 a1.txt
[root@root test01]# cp -a test3 test4
[root@root test01]# ll test4
总用量 8
-rw-r--r-- 1 root root    0 1月  21 10:24 2020.txt
-rw-r--r-- 1 root root    0 2月  11 2021 2021.log
-rw-r--r-- 1 root root    0 1月  15 17:00 21.java
-rw-r--r-- 1 root root    0 1月  21 10:24 31.log
-rw-r--r-- 1 root root    0 1月  15 17:00 32.log
-rw-r--r-- 1 root root  317 1月  21 15:11 a1.txt
drwxr-xr-x 2 root root 4096 1月  18 16:02 test3
---------------------------------------------------------------
目标目录不存在时:(在当前目录创建一个目标目录,把原目录的文件复制到目标目录)
# cp -a test3 test5
[root@root test01]# ll
总用量 44
-rw-r--r-- 1 root root   317 1月  15 17:53 a1.txt
-rw-r--r-- 1 root root     0 1月  15 17:47 a2.txt
-rw-r--r-- 1 root root 10240 1月  18 14:50 aaa.tar
-rw-r--r-- 1 root root     0 1月  18 14:51 aaa.tar.bz2
-rw-r--r-- 1 root root   296 1月  18 14:49 aaa.tar.gz
-rw-r--r-- 1 root root     0 1月  15 17:47 b1.java
drwxr-xr-x 2 root root  4096 1月  18 11:07 test1
drwxr-xr-x 2 root root  4096 1月  18 18:18 test2
drwxr-xr-x 2 root root  4096 1月  18 16:02 test3
drwxr-xr-x 3 root root  4096 1月  21 15:36 test4
drwxr-xr-x 2 root root  4096 1月  18 16:02 test5
drwxr-xr-x 4 root root  4096 1月  18 17:44 备份
[root@root test01]# ll test5
总用量 4
-rw-r--r-- 1 root root 118 1月  18 16:01 2122.tar.gz
-rw-r--r-- 1 root root   0 1月  15 17:00 21.java
-rw-r--r-- 1 root root   0 1月  15 17:00 22.java
-------------------------------------------------------------------------------------------------
3.复制的a1.txt建立一个连结档a1_link.txt:
# cp -s a1.txt a1_link.txt
[root@root test01]# ll
总用量 44
lrwxrwxrwx 1 root root     6 1月  21 15:53 a1_link.txt -> a1.txt
-rw-r--r-- 1 root root   317 1月  15 17:53 a1.txt
-rw-r--r-- 1 root root     0 1月  15 17:47 a2.txt
-rw-r--r-- 1 root root 10240 1月  18 14:50 aaa.tar
-rw-r--r-- 1 root root     0 1月  18 14:51 aaa.tar.bz2
-rw-r--r-- 1 root root   296 1月  18 14:49 aaa.tar.gz
-rw-r--r-- 1 root root     0 1月  15 17:47 b1.java
drwxr-xr-x 2 root root  4096 1月  18 11:07 test1
drwxr-xr-x 2 root root  4096 1月  18 18:18 test2
drwxr-xr-x 2 root root  4096 1月  18 16:02 test3
drwxr-xr-x 3 root root  4096 1月  21 15:36 test4
drwxr-xr-x 2 root root  4096 1月  18 16:02 test5
drwxr-xr-x 4 root root  4096 1月  18 17:44 备份
-------------------------------------------------------------------------------------------------
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值