Linux命令之cp
cp [选项] [–T] 源文件 目标文件
cp [选项] 源文件 目录
cp [选项] –t 目录 源文件
将源文件复制至目标文件,或将多个源文件复制至目标目录
(1).常用选项
补充:符号链接又叫软链接,是一类特殊的文件,这个文件包含了另一个文件的路径名(绝对路径或相对路径)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | -a 等于 –dR –preserve=all --backup 为每个已存在的目标文件创建备份 -b 类似于 --backup 但不接受参数 -d 等于 --no-dereference –preserve=links -f 如果目标文件无法打开则将其移除并重试(当-n存在时则不需要再选此参数) -i 覆盖前询问(使前面的-n参数失效) -n 不要覆盖已存在的文件(使前面的-i参数失效) -P,--no-dereference 不跟随源文件中的符号链接 --preserve 保持指定的属性(默认:模式,所有权,时间戳),如果可能保持附加属性:环境、链接、xattr(文件系统的扩展属性)等 -R,-r,--recursive 递归复制目录及其子目录内的所有内容 -s 只创建符号链接而不复制文件 -t 将所有参数指定的源文件/目录复制至目标目录 -T 将目标目录视作普通文件 -v 说明正在做什么(详细显示命令执行操作) |
(2).实例
补充:mkdir用来创建目录;ll约等于ls -l
将文档拷贝到另一个文件夹下,该文件夹下没有同名文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [ root@CentOS6 桌面]# mkdir cpDir [ root@CentOS6 桌面]# ll 总用量 4 drwxr - xr - x . 2 root root 4096 4月 3 12:34 cpDir [ root@CentOS6 桌面]# cat >mytext << EOF > this is mytext! > EOF [ root@CentOS6 桌面]# ll 总用量 8 drwxr - xr - x. 2 root root 4096 4月 3 12:34 cpDir - rw- r - - r- - . 1 root root 16 4月 3 13:03 mytext [ root@CentOS6 桌面]# cp mytext cpDir [ root@CentOS6 桌面]# ls –l ./cpDir 总用量 4 - rw- r - - r- -. 1 root root 16 4月 3 13:05 mytext [ root@CentOS6 桌面]# cat mytext this is mytext! [ root@CentOS6 桌面]# ll 总用量 8 drwxr - xr - x. 2 root root 4096 4月 3 13:05 cpDir - rw- r - - r- - . 1 root root 16 4月 3 13:03 mytext |
将文档拷贝到另一个文件夹下,该文件夹下有同名文档,因此会提醒是否覆盖。
1 2 3 4 5 6 7 8 9 10 11 | [ root@CentOS6 桌面]# ll 总用量 8 drwxr - xr - x. 2 root root 4096 4月 3 13:05 cpDir - rw- r - - r- - . 1 root root 16 4月 3 13:03 mytext [ root@CentOS6 桌面]# cat >mytext << EOF > Modify the text ! > EOF [ root@CentOS6 桌面]# cp mytext cpDir cp:是否覆盖“cpDir/mytext”? y [ root@CentOS6 桌面]# cat ./cpDir/mytext Modify the text ! |
将newDir拷贝一份到Dir1目录下(当Dir1文件夹存在时);将newDir下的所有东西拷贝一份到新建的Dir2目录(Dir2不存在)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | [ root@CentOS6 桌面]# ll 总用量 0 [ root@CentOS6 桌面]# mkdir newDir [ root@CentOS6 桌面]# touch ./newDir/{text1.txt,text2.txt} [ root@CentOS6 桌面]# ll 总用量 4 drwxr - xr - x. 2 root root 4096 4月 3 15:28 newDir [ root@CentOS6 桌面]# mkdir Dir1 [ root@CentOS6 桌面]# cp newDir Dir1 cp:略过目录“newDir” [ root@CentOS6 桌面]# cp -a newDir Dir1 //存在Dir1 [ root@CentOS6 桌面]# cp -a newDir Dir2 //不存在Dir2 [ root@CentOS6 桌面]# ll 总用量 12 drwxr - xr - x. 3 root root 4096 4月 3 15:29 Dir1 drwxr - xr - x. 2 root root 4096 4月 3 15:28 Dir2 drwxr - xr - x. 2 root root 4096 4月 3 15:28 newDir [ root@CentOS6 桌面]# ls ./Dir1 //存在的效果 newDir [ root@CentOS6 桌面]# ls ./Dir2 //不存在的效果 text1.txt text2.txt |
建立一个指向text1.txt的快捷方式 :t1_link
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | [ root@CentOS6 桌面]# cp –s newDir newDir_link //只有-s无法创建文件夹的快捷方式 cp:略过目录“newDir” [ root@CentOS6 桌面]# ll 总用量 12 drwxr - xr - x. 3 root root 4096 4月 3 15:29 Dir1 drwxr - xr - x. 2 root root 4096 4月 3 15:28 Dir2 drwxr - xr - x. 2 root root 4096 4月 3 15:28 newDir [ root@CentOS6 桌面]# cd newDir [ root@CentOS6 newDir]# ll 总用量 0 - rw- r- - r- -. 1 root root 0 4月 3 15:28 text1.txt - rw- r- - r- -. 1 root root 0 4月 3 15:28 text2.txt [ root@CentOS6 newDir]# cp –s text1.txt t1_link [ root@CentOS6 newDir]# ll 总用量 0 lrwxrwxrwx. 1 root root 9 4月 4 08:33 t1_link - > text1.txt -rw- r- - r- -. 1 root root 0 4月 3 15:28 text1.txt -rw- r- - r- -. 1 root root 0 4月 3 15:28 text2.txt |
创建文件夹的快捷方式
1 2 3 4 5 6 7 8 9 10 | [ root@CentOS6 桌面]# cp –as newDir newDir_link cp:“newDir_link/t1_link”:只能用于当前目录中创建相对的符号链接 cp:“newDir_link/text1.txt”:只能用于当前目录中创建相对的符号链接 cp:“newDir_link/text2.txt”:只能用于当前目录中创建相对的符号链接 [ root@CentOS6 桌面]# ll 总用量 16 drwxr - xr - x. 3 root root 4096 4月 3 15:29 Dir1 drwxr - xr - x. 2 root root 4096 4月 3 15:28 Dir2 drwxr - xr - x. 2 root root 4096 4月 3 15:28 newDir drwxr - xr - x. 2 root root 4096 4月 3 15:28 newDir_link |
复制一份加上后缀存起来
1 2 3 4 5 6 7 8 9 10 | [ root@CentOS6 桌面]# touch mytext [ root@CentOS6 桌面]# cp mytext{,.txt} [ root@CentOS6 桌面]# ll 总用量 16 drwxr - xr - x. 3 root root 4096 4月 3 15:29 Dir1 drwxr - xr - x. 2 root root 4096 4月 3 15:28 Dir2 -rw- r - - r - - . 1 root root 0 4月 4 10:42 mytext -rw- r - - r - - . 1 root root 0 4月 4 10:42 mytext.txt drwxr - xr - x. 2 root root 4096 4月 4 15:28 newDir drwxr - xr - x. 2 root root 4096 4月 4 15:28 newDir_link |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性