一天一条Linux指令-cp
来源:互联网 发布:微信淘宝链接转化 编辑:程序博客网 时间:2024/04/29 16:49
用途说明
cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一。一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是在shell脚本中执行cp时,没有-i参数时不会询问是否覆盖。这说明命令行和shell脚本的执行方式有些不同。
[root@jfht ~]# type -a cp
cp is aliased to `cp -i' <== 在命令行状态,cp实际上是个别名
cp is /bin/cp
[root@jfht ~]#
常用参数
-i 询问是否覆盖。
-f 强制覆盖,但如果有-i参数时,此参数无效。
-r 递归复制。
-a 递归复制,并且保留文件的时间、权限、属主等属性。
使用示例
示例一 复制单个文件
[root@jfht ~]# ls >ls0.txt
[root@jfht ~]# ls -l ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:41 ls0.txt
等待两分钟再操作。因为同一分钟看不出效果。
[root@jfht ~]# cp ls0.txt ls1.txt
[root@jfht ~]# ls -l ls0.txt ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:41 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 ls1.txt
在没有带-a参数时,两个文件的时间是不一样的。
[root@jfht ~]# cp -a ls0.txt ls2.txt
[root@jfht ~]# ls -l ls0.txt ls2.txt
-rw-r--r-- 1 root root 213 10-18 20:41 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:41 ls2.txt
在带了-a参数时,两个文件的时间是一致的。
[root@jfht ~]# cp ls0.txt ls2.txt
cp:是否覆盖“ls2.txt”? n
目标文件存在时,会询问是否覆盖。这是因为cp是cp -i的别名。
[root@jfht ~]# cp -f ls0.txt ls2.txt
cp:是否覆盖“ls2.txt”? n
目标文件存在时,即使加了-f标志,也还会询问是否覆盖。
[root@jfht ~]# command cp ls0.txt ls2.txt
使用command命令执行cp时,就不会执行cp别名了,而是直接执行cp外部命令。
[root@jfht ~]# ls -l ls0.txt ls2.txt
-rw-r--r-- 1 root root 213 10-18 20:41 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:48 ls2.txt
[root@jfht ~]# cp ls0.txt ls0.txt
cp: “ls0.txt” 及 “ls0.txt” 为同一文件
[root@jfht ~]# cp -f ls0.txt ls0.txt
cp: “ls0.txt” 及 “ls0.txt” 为同一文件
如果目标文件就是源文件,那么无论加不加-f标志,都是不会覆盖的。
示例二 复制文件到目录
[root@jfht ~]# mkdir tmp
[root@jfht ~]# cp ls0.txt tmp
[root@jfht ~]# cp -a ls1.txt tmp
[root@jfht ~]# cp -a ls1.txt ls2.txt tmp
cp:是否覆盖“tmp/ls1.txt”? n
[root@jfht ~]# ls -l ls?.txt tmp/ls?.txt
-rw-r--r-- 1 root root 213 10-18 20:41 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:48 ls2.txt
-rw-r--r-- 1 root root 213 10-18 21:02 tmp/ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 tmp/ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:48 tmp/ls2.txt
[root@jfht ~]#
示例三 复制整个目录
[root@jfht ~]# cp tmp tmp1
cp: 略过目录 “tmp”
[root@jfht ~]# cp -a tmp tmp1
[root@jfht ~]# ls tmp tmp1
tmp:
ls0.txt ls1.txt ls2.txt
tmp1:
ls0.txt ls1.txt ls2.txt
[root@jfht ~]# ls -l tmp tmp1
tmp:
总计 24
-rw-r--r-- 1 root root 213 10-18 21:02 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:48 ls2.txt
tmp1:
总计 24
-rw-r--r-- 1 root root 213 10-18 21:02 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:48 ls2.txt
[root@jfht ~]#
[root@jfht ~]# cp -a tmp tmp1
[root@jfht ~]# ls -l tmp tmp1
tmp:
总计 24
-rw-r--r-- 1 root root 213 10-18 21:02 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:48 ls2.txt
tmp1:
总计 32
-rw-r--r-- 1 root root 213 10-18 21:02 ls0.txt
-rw-r--r-- 1 root root 213 10-18 20:43 ls1.txt
-rw-r--r-- 1 root root 213 10-18 20:48 ls2.txt
drwxr-xr-x 2 root root 4096 10-18 21:03 tmp
[root@jfht ~]#
注意目标目录存在与否结果是不一样的。目标目录存在时,整个源目录被复制到目标目录里面。
示例四 将光盘复制成光盘映像文件(iso文件)
把要复制的光盘放在光驱中,注意不需要将/dev/cdrom设备mount上来。
[root@localhost data] cp /dev/cdrom disk1.iso
[root@localhost data] ls -l disk1.iso
-rw-r----- 1 root root 655392768 10月 18 19:48 disk1.iso
[root@localhost data]
注意:需要好几分钟时间。
- 一天一条Linux指令-cp
- 一天一条Linux指令-:
- 一天一条Linux指令-cd
- 一天一条Linux指令-apt
- 一天一条Linux指令-mount
- 一天一条Linux指令-ls
- 一天一条Linux指令-mkdir
- 一天一条Linux指令-cat
- 一天一条Linux指令-find
- 一天一条Linux指令-umount
- 一天一条Linux指令-cpu
- 一天一条Linux指令-CPU
- 一天一条Linux指令-file
- 一天一条Linux指令-mv
- 一天一条Linux指令-ln
- 一天一条Linux指令-dd
- 一天一条Linux指令-压缩
- 一天一条Linux指令-关机
- 设置EditText设置为两位小数,或者其他位数
- python虚拟环境搭建
- WebAPI 用ExceptionFilterAttribute实现错误(异常)日志
- 操作系统与网络实现 之一(丙)
- Codeforces 365Div2 D Mishka and Interesting sum
- 一天一条Linux指令-cp
- ubuntu16.10下JDK8的安装与配置
- Axis2之HelloWorld
- jsp生成 文件下载 比如txt
- cordova cordova-plugin-inappbrowser net:ERR_UNKNOWN_URL_SCHEME
- Android微信智能心跳方案
- Servlet连接数据库
- mysql保存中文乱码的原因和解决办法
- 获取百度开放平台定位的方法