linux下shell脚本⾥写cp,⼀个简单的linux命令cp
cp命令⽤来复制⽂件或者⽬录,是Linux系统中最常⽤的命令之⼀。⼀般情况下,shell会设置⼀个别名,在命令⾏下复制⽂件时,如果⽬标⽂件已经存在,就会询问是否覆盖,不管你是否使⽤-i参数。但是如果是在shell脚本中执⾏cp时,没有-i参数时不会询问是否覆盖。这说明命令⾏和shell脚本的执⾏⽅式有些不同。
命令格式
cp [选项]… [-T] 源 ⽬的
命令参数
-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 复制⽬录及⽬录内的所有项⽬
命令范例
实例⼀:复制单个⽂件到⽬标⽬录,⽂件在⽬标⽂件中不存在
命令:
cp log.log test5输出:
[root@localhost test]# cp log.log test5
[root@localhost test]# ll
-rw-r–r– 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 2 root root 4096 10-28 14:53 test5
[root@localhost test]# cd test5
[root@localhost test5]# ll
-rw-r–r– 1 root root 0 10-28 14:46 log5-1.log
-rw-r–r– 1 root root 0 10-28 14:46 log5-2.log
-rw-r–r– 1 root root 0 10-28 14:46 log5-3.log
-rw-r–r– 1 root root 0 10-28 14:53 log.log
说明:
在没有带-a参数时,两个⽂件的时间是不⼀样的。在带了-a参数时,两个⽂件的时间是⼀致的。
实例⼆:⽬标⽂件存在时,会询问是否覆盖
命令:
cp log.log test5输出:
[root@localhost test]# cp log.log test5
cp:是否覆盖“test5/log.log”? n
[root@localhost test]# cp -a log.log test5
cp:是否覆盖“test5/log.log”? y
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r–r– 1 root root 0 10-28 14:46 log5-1.log
-rw-r–r– 1 root root 0 10-28 14:46 log5-2.log
-rw-r–r– 1 root root 0 10-28 14:46 log5-3.log
-rw-r–r– 1 root root 0 10-28 14:48 log.log
说明:shell最简单脚本
⽬标⽂件存在时,会询问是否覆盖。这是因为cp是cp -i的别名。⽬标⽂件存在时,即使加了-f标志,也还会询问是否覆盖。实例三:复制整个⽬录
命令:cp -a test3 test5输出:
⽬标⽬录存在时:
[root@localhost test]# cp -a test3 test5
[root@localhost test]# ll
-rw-r–r– 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]# cd test5/
[root@localhost test5]# ll
-rw-r–r– 1 root root 0 10-28 14:46 log5-1.log
-rw-r–r– 1 root root 0 10-28 14:46 log5-2.log
-rw-r–r– 1 root root 0 10-28 14:46 log5-3.log
-rw-r–r– 1 root root 0 10-28 14:48 log.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
⽬标⽬录不存在是:
[root@localhost test]# cp -a test3 test4
[root@localhost test]# ll
-rw-r–r– 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#
说明:
注意⽬标⽬录存在与否结果是不⼀样的。⽬标⽬录存在时,整个源⽬录被复制到⽬标⽬录⾥⾯。
实例四:复制的 log.log 建⽴⼀个连结档 log_link.log
命令:
cp -s log.log log_link.log输出:
[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# ll
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -> log.log
-rw-r–r– 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
说明:
那个 log_link.log 是由 -s 的参数造成的,建⽴的是⼀个××『快捷⽅式』××,所以您会看到在⽂件的最右边,会显⽰这个⽂件是『连结』到哪⾥去的!
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持脚本之家。