每天一个linux命令:cp(8)
cp
cp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误
格式
cp [选项] [参数]
参数选项
参数 | 备注 |
---|---|
-a | 此参数的效果和同时指定"-dpR"参数相同; |
-d | 当复制符号连接时,把目标文件或目录也建立为符号连接,并指向与源文件或目录连接的原始文件或目录; |
-f | 强行复制文件或目录,不论目标文件或目录是否已存在; |
-i | 覆盖既有文件之前先询问用户; |
-l | 对源文件建立硬连接,而非复制文件; |
-p | 保留源文件或目录的属性; |
-R/r | 递归处理,将指定目录下的所有文件与子目录一并处理; |
-s | 对源文件建立符号连接,而非复制文件; |
-u | 使用这项参数后只会在源文件的更改时间较目标文件更新时或是名称相互对应的目标文件并不存在时,才复制文件; |
-S | 在备份文件时,用指定的后缀“SUFFIX”代替文件的默认后缀; |
-b | 覆盖已存在的文件目标前将目标文件备份; |
-v | 详细显示命令执行的操作。 |
- 源文件:制定源文件列表。默认情况下,cp命令不能复制目录,如果要复制目录,则必须使用
-R
选项; - 目标文件:指定目标文件。当“源文件”为多个文件时,要求“目标文件”为指定的目录。
实例
复制单个文件到目标目录,文件在目标文件中不存在
命令: cp myFile ./targetDir/myFile
[root@VM_0_9_centos test]# mkdir targetDir && cp myFile ./targetDir/myFile
[root@VM_0_9_centos test]# tree
.
|-- myFile
`-- targetDir
`-- myFile #复制的文件
复制单个文件到目标目录下并改名
命令: cp myFile ./targetDir/myFile2
[root@VM_0_9_centos test]# mkdir targetDir && cp myFile ./targetDir/myFile2
[root@VM_0_9_centos test]# tree
.
|-- myFile
`-- targetDir
`-- myFile2 #复制的文件
目标文件存在时,会询问是否覆盖
命令:**cp myFile ./targetDir/myFile2 **
[root@VM_0_9_centos test]# cp myFile ./targetDir/myFile2
cp: overwrite ?./targetDir/myFile2?. y
复制整个目录
命令: cp -a sourceDir targetDir/ cp -r sourceDir targetDir
[root@VM_0_9_centos test]# tree
.
|-- sourceDir
| |-- myFile1
| `-- myFile2
`-- targetDir
2 directories, 2 files
[root@VM_0_9_centos test]# cp -a sourceDir targetDir
[root@VM_0_9_centos test]# tree
.
|-- sourceDir
| |-- myFile1
| `-- myFile2
`-- targetDir
`-- sourceDir
|-- myFile1
`-- myFile2
3 directories, 4 files
复制的文件时,只生成连接文档,而非真正的复制
命令:cp -s test.txt test_link.txt
root@VM_0_9_centos test]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 27 23:05 sourceDir
drwxr-xr-x 3 root root 4096 Oct 27 23:38 targetDir
[root@VM_0_9_centos test]# touch test.txt
[root@VM_0_9_centos test]# cp -s test.txt test_link.txt
[root@VM_0_9_centos test]# ll
total 8
drwxr-xr-x 2 root root 4096 Oct 27 23:05 sourceDir
drwxr-xr-x 3 root root 4096 Oct 27 23:38 targetDir
lrwxrwxrwx 1 root root 8 Oct 27 23:44 test_link.txt -> test.txt
-rw-r--r-- 1 root root 0 Oct 27 23:44 test.txt
拷贝覆盖文件,避免出现确认提示y的方法
命令:**\cp -a sourceDir targetDir **
[root@VM_0_9_centos test]# tree
.
|-- sourceDir
| |-- myFile1
| `-- myFile2
`-- targetDir
`-- sourceDir
|-- myFile1
`-- myFile2
3 directories, 4 files
[root@VM_0_9_centos test]# ls -l targetDir/sourceDir/
total 0
-rw-r--r-- 1 root root 0 Oct 27 23:05 myFile1
-rw-r--r-- 1 root root 0 Oct 27 23:05 myFile2
[root@VM_0_9_centos test]# cp -a sourceDir targetDir
cp: overwrite ?.argetDir/sourceDir/myFile1?. y
cp: overwrite ?.argetDir/sourceDir/myFile2?. y
[root@VM_0_9_centos test]# \cp -a sourceDir targ etDir
[root@VM_0_9_centos test]# tree
.
|-- sourceDir
| |-- myFile1
| `-- myFile2
`-- targetDir
`-- sourceDir
|-- myFile1
`-- myFile2
3 directories, 4 files
参考
每天一个linux命令:cp(8)的更多相关文章
- 每天一个 Linux 命令(21):find命令之xargs
在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...
- 每天一个linux命令目录
出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...
- 每天一个 Linux 命令(20):find命令之exec
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- 每天一个linux命令(20):find命令之exec
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是command ...
- 每天一个Linux命令
每天一个Linux命令(1):ls命令 每天一个Linux命令(2):cd命令 每天一个Linux命令(3):pwd命令 每天一个 Linux 命令(4):mkdir 每天一个 Linux 命令(5) ...
- 每天一个linux命令-转载
每天一个linux命令目录 转载自: http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令 ...
- [转]每天一个linux命令目录
[转]每天一个linux命令目录 http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每 ...
- 每天一个Linux命令(20)--find命令之exec
find 是我们很常用的一个Linux命令,但是我们一般查找出来的额并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是 comm ...
- 每天一个Linux命令 (转)
一. 文件目录操作命令: 1.每天一个linux命令(1):ls命令 2.每天一个linux命令(2):cd命令 3.每天一个linux命令(3):pwd命令 4.每天一个linux命令(4):mk ...
随机推荐
- python 列表使用
下面实现的类似于java中的数组: names[-2]表示实现倒数的第2个参数 names[-3,-1]表示实现-3到-1的值不包含-1 增删改查 下面代码实现列表的增删改查功能: 复制copy 深c ...
- Vue学习笔记-组件通信-父传子(props中的驼峰标识)
在组件中,使用选项props来声明需要从父级接收到的数据.props的值有两种方式:方式一:字符串数组,数组中的字符串就是传递时的名称.方式二:对象,对象可以设置传递时的类型,也可以设置默认值等. & ...
- Android USB驱动源码分析(-)
Android USB驱动中,上层应用协议里最重要的一个文件是android/kernel/drivers/usb/gadget/android.c.这个文件实现USB的上层应用协议. 首先包含了一些 ...
- SpringMvc返回JSON的工作原理。
一:导入jar包.然后在springmvc.xml中配置上这么一句话 <!-- 能支持springmvc更高级的一些功能,JSR303校验,快捷的ajax,处理JSON数据.映射动态请求 --& ...
- shell脚本学习 (9) 提取开头或结尾的几行
1 提取开头的n行 用head awk或者 sed实现 do.txt sed 1q do.txt awk 'FNR <= 1' do.txt do.txt文件 2 显示行尾的几行 用tail - ...
- 08-图8 How Long Does It Take(25 分)邻接表和队列
Given the relations of all the activities of a project, you are supposed to find the earliest comple ...
- [ZJOI2019]开关
以下的方案数默认是带权方案数.设\(P=\sum_{i=1}^np_i\). 设\(F(x)\)为按\(i\)次开关后到达终止态的方案数的EGF,\(f\)为\(F\)的OGF,显然\(F(x)=\p ...
- cf1278D——树的性质+并查集+线段树/DFS判环
昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...
- Docker(应用服务引擎)
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何 ...
- ansible控制winserver笔记
原文地址: https://www.cnblogs.com/kingleft/p/6391652.html 环境描述: ansible控制远程windows .系统必须是sp1 .安装framewor ...