一个简单的linux命令 cp

所属分类: 脚本专栏 / linux shell 阅读数: 1102
收藏 0 赞 0 分享

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 

说明:
目标文件存在时,会询问是否覆盖。这是因为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 的参数造成的,建立的是一个××『快捷方式』××,所以您会看到在文件的最右边,会显示这个文件是『连结』到哪里去的!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

linux Shell入门:掌握Linux,OS X,Unix的Shell环境

这篇文章主要介绍了linux Shell入门:掌握Linux,OS X,Unix的Shell环境 ,需要的朋友可以参考下
收藏 0 赞 0 分享

shell 1>&2 2>&1 &>filename重定向的含义和区别

这篇文章主要介绍了shell 1>&2 2>&1 &>filename重定向的含义和区别,需要的朋友可以参考下
收藏 0 赞 0 分享

ssh远程执行命令方法和Shell脚本实例

这篇文章主要介绍了ssh远程执行命令方法和Shell脚本实例,本文讲解了ssh执行远程操作方法和远程执行命令shell脚本示例,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell中的${}、##和%%使用范例

这篇文章主要介绍了Shell中的${}、##和%%使用范例,本文给出了不同情况下得到的结果,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本一次读取文件中一行的2种写法

这篇文章主要介绍了Shell脚本一次读取文件中一行的2种写法,本文还同时讲解了Shell读取文本文件的2种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本逐行读取文本文件(不改变文本格式)

这篇文章主要介绍了Shell脚本逐行读取文本文件,本文着重探讨不改变文本格式的方法读取出文件内容,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本配合iptables屏蔽来自某个国家的IP访问

这篇文章主要介绍了Shell脚本配合iptables屏蔽来自某个国家的IP访问,本文利用IPdeny的IP数据,然后用Shell脚本导入iptables实现屏蔽IP访问,需要的朋友可以参考下
收藏 0 赞 0 分享

Bash脚本内置的调试方法技巧

这篇文章主要介绍了Bash脚本内置的调试方法技巧,本文介绍了调试技巧和几个调试技巧,比如输出行号的方法、只调试某段程序的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell、Perl、Python、PHP访问 MySQL 数据库代码实例

这篇文章主要介绍了Shell、Perl、Python、PHP访问 MySQL 数据库代码实例,本文分别给出这几种语言访问Mysql数据的代码实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Shell脚本计算字符串长度和判断字符串为空小技巧

这篇文章主要介绍了Shell脚本计算字符串长度和判断字符串为空小技巧,本文分别给出计算字符串长度和判断字符串为空各3种实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多