cp:复制文件或者目录

用法格式:

cp [option] [source] [dest]

cp [选项] [源文件] [目标文件]

>用root账户,创建文件,复制文件

root@dev:/home/ghostwu/linux/cp# vim 1.txt
root@dev:/home/ghostwu/linux/cp# ls -l
total 4
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
root@dev:/home/ghostwu/linux/cp# cp 1.txt 2.txt
root@dev:/home/ghostwu/linux/cp# ls -l
total 8
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
root@dev:/home/ghostwu/linux/cp# su - ghostwu
ghostwu@dev:~$ cd -
-su: cd: OLDPWD not set
ghostwu@dev:~$ cd linux/cp
ghostwu@dev:~/linux/cp$ ls -l
total 8
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
ghostwu@dev:~/linux/cp$ cp 2.txt 3.txt
cp: cannot create regular file '3.txt': Permission denied

上面,当我切换到ghostwu这个账户去复制的时候,权限不允许,因为2.txt 这个文件 的其他组只有 只读 权限, 而cp需要写权限,所以就报了一个无权限创建复制的文件。

方法一,用sudo提权

ghostwu@dev:~/linux/cp$ ls -l
total 8
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
ghostwu@dev:~/linux/cp$ sudo cp 2.txt 3.txt
[sudo] password for ghostwu:
ghostwu@dev:~/linux/cp$ ls -l
total 12
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
-rw-r--r-- 1 root root 19 5月   6 17:52 3.txt

方法二,用root用户给文件的其他组用户 可写权限,同时普通用户要对文件所属的目录拥有写权限, 也就是要对 "cp" 这个目录拥有写权限

ghostwu@dev:~/linux$ ls -l
total 4
drwxr-xr-x 2 root root 4096 5月   6 17:52 cp
ghostwu@dev:~/linux$ sudo chmod o+w cp
ghostwu@dev:~/linux$ ls -l
total 4
drwxr-xrwx 2 root root 4096 5月   6 17:52 cp
ghostwu@dev:~/linux$ cd cp
ghostwu@dev:~/linux/cp$ ls -l
total 12
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--r-- 1 root root 19 5月   6 17:48 2.txt
-rw-r--rw- 1 root root 19 5月   6 17:52 3.txt
ghostwu@dev:~/linux/cp$ sudo chmod o+w 2.txt
ghostwu@dev:~/linux/cp$ ls -l
total 12
-rw-r--r-- 1 root root 19 5月   6 17:47 1.txt
-rw-r--rw- 1 root root 19 5月   6 17:48 2.txt
-rw-r--rw- 1 root root 19 5月   6 17:52 3.txt
ghostwu@dev:~/linux/cp$ cp 2.txt 4.txt
ghostwu@dev:~/linux/cp$ ls -l
total 16
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt

用普通用户去复制root账户创建的2.txt文件,起一个新名字4.txt,默认情况下cp 改变了文件的权限和时间属性,如果在复制的时候想保留文件原有的权限信息以及时间属性时,可以加参数 -p

ghostwu@dev:~/linux/cp$ ls -l
total 16
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
ghostwu@dev:~/linux/cp$ cp -p 2.txt 5.txt
ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月   6 17:48 5.txt

-i: 带提示信息的复制,默认情况下,cp命令会直接覆盖

ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月   6 17:48 5.txt
ghostwu@dev:~/linux/cp$ cp 2.txt 5.txt
ghostwu@dev:~/linux/cp$ cp -i 2.txt 5.txt
cp: overwrite '5.txt'? y

-r参数: 递归复制目录以及文件

ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root    root    19 5月   6 17:47 1.txt
-rw-r--rw- 1 root    root    19 5月   6 17:48 2.txt
-rw-r--rw- 1 root    root    19 5月   6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月   6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月   6 18:04 5.txt
ghostwu@dev:~/linux/cp$ mkdir -p a/b
ghostwu@dev:~/linux/cp$ mv *.txt a/b/
ghostwu@dev:~/linux/cp$ tree
.
└── a└── b├── 1.txt├── 2.txt├── 3.txt├── 4.txt└── 5.txt2 directories, 5 files
ghostwu@dev:~/linux/cp$ cp a a2
cp: omitting directory 'a'
ghostwu@dev:~/linux/cp$ ls
a
ghostwu@dev:~/linux/cp$ cp -r a a2
ghostwu@dev:~/linux/cp$ tree
.
├── a
│   └── b
│       ├── 1.txt
│       ├── 2.txt
│       ├── 3.txt
│       ├── 4.txt
│       └── 5.txt
└── a2└── b├── 1.txt├── 2.txt├── 3.txt├── 4.txt└── 5.txt4 directories, 10 files
ghostwu@dev:~/linux/cp$ 

通过alias别名,给cp命令加提示信息

ghostwu@dev:~/linux/cp$ alias cp='cp -i'
ghostwu@dev:~/linux/cp$ ls
a  a2
ghostwu@dev:~/linux/cp$ touch 1.txt
ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt
ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt
cp: overwrite '2.txt'? y
ghostwu@dev:~/linux/cp$ 

使用命令的绝对路径(全路径),可以屏蔽别名

ghostwu@dev:~/linux/cp$ alias | grep cp
alias cp='cp -i'
ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2
ghostwu@dev:~/linux/cp$ cp 1.txt 2.txt
cp: overwrite '2.txt'? y
ghostwu@dev:~/linux/cp$ which cp
/bin/cp
ghostwu@dev:~/linux/cp$ /bin/cp 1.txt 2.txt 

使用反斜杠,也可以屏蔽系统别名

ghostwu@dev:~/linux/cp$ \cp 1.txt 2.txt
ghostwu@dev:~/linux/cp$ \cp 2.txt 1.txt 

-a参数,相当于-r -d -p三个参数的综合作用效果

ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2
ghostwu@dev:~/linux/cp$ cp -a a a3
ghostwu@dev:~/linux/cp$ ls
1.txt  2.txt  a  a2  a3

Linux常用基本命令[cp]相关推荐

  1. 【Linux】linux常用基本命令

    首页 博客 学院 CSDN学院 下载 论坛 APP CSDN 问答 商城 活动 VIP会员 招聘 ITeye GitChat GitChat 图文课 写博客 消息 评论关注点赞回答系统通知 登录注册 ...

  2. Linux常用基本命令详解(一)

    Linux常用基本命令详解(一) Linux常用基本命令详解(二)-------磁盘分区和磁盘管理类命令 Linux常用基本命令详解(三) 1.帮助命令 1.1.man命令 man[命令或配置文件] ...

  3. Linux常用基本命令详解(二)-------磁盘分区和磁盘管理类命令

    Linux常用基本命令详解(一) Linux常用基本命令详解(二)-------磁盘分区和磁盘管理类命令 Linux常用基本命令详解(三) 1.磁盘分区 磁盘分区(系统分区)是使用分区编辑器(part ...

  4. linux常用命令 cp命令的使用和介绍

    linux常用命令 cp命令的使用和介绍 1.从一台远程的linux服务器上复制文件到本机服务器

  5. 布丁浅谈之Linux常用基本命令

    常用基本命令 VI VIM编辑器 概念:是Linux系统命令行下的文本编辑器. 一般模式 dd 删除光标当前行 dnd 删除n行 u 撤销上一步 x 删除一个字母,类似于键盘上Delete功能 X 删 ...

  6. 【LINUX 常用基本命令】--最全最详细整理

     常用基本命令 1 帮助命令 1.1 man 获得帮助信息 1)基本语法 man [命令或配置文件] (功能描述:获得帮助信息) 2)显示说明 表1-6 信息 功能 NAME 命令的名称和单行描述 S ...

  7. linux常用基本命令大全(超详细,建议多操作,多练)

    linux常用命令 在Linux中存在绝对路径和相对路径. 绝对路径:路径的写法一定由根目录 '/'写起,例如/usr/local/net-snmp. 相对路径:路径的写法不是由根目录 '/'写起.例 ...

  8. Linux常用基本命令分享

    思维导图分享 思维导图中的命令若不会使用,可查看下方的案例.需要原文件可私聊. 一.帮助命令 1.1 man 获得帮助信息 基本语法 ​ man [命令或配置文件] (功能描述:获得帮助信息) ​ q ...

  9. linux常用基本命令

    本人菜鸟一枚,但是我想飞高点,去尝尝白云是什么味道.先搞这么多吧 目录 1.cd 2. pwd 3. ls 4.touch 5.cp 6.ln 7.mv 8.rm 9.mkdir 10.rmdir 1 ...

最新文章

  1. MPB:亚热带生态所谭支良组-基于微生物成分数据的差异zOTU分析流程
  2. ubuntu 安装 lamp 环境
  3. python UnicodeEncodeError: 'gbk' codec can't encode character ...
  4. debian nvidia 安装_【折腾】openSUSE安装与配置——从入门到放弃
  5. 云计算物联网Hold住未来十大技术趋势
  6. 成功解决schedule.ScheduleValueError: Invalid time format
  7. 第一个SSM整合的Maven入门级项目(超详细步骤)
  8. android 如何用httpclient发请求和利用httphead头信息给服务器
  9. IIC原理及简单流程
  10. C#LeetCode刷题之#242-有效的字母异位词(Valid Anagram)
  11. 【云计算平台】Hadoop单机模式环境搭建
  12. LeetCode之搜索旋转排序数组
  13. lcmgcd因数分解
  14. 还原卡及还原精灵的破解
  15. html带圈的数字号码,html – 带有数字的CSS圈子
  16. Java集成建行龙支付接口(详细)
  17. IP归属地显示的简单实现代码
  18. 数据报表、数据分析、数据挖掘和商业智能,是什么关系?
  19. Docker部署程序员简历
  20. Fragstats计算景观格局指数不出结果和分维数PAFRAC出现N/A的问题

热门文章

  1. linux虚拟用户创建目录权限不足,在CentOs中安装vsFtpd并创建多个虚拟用户,且不同的用户拥有不同的权限以及指向不同的文件夹...
  2. 算法炒房三月亏20多亿。房地产巨头大翻车:房价水太深,AI根本把握不住
  3. CNN、RNN、GAN都是什么?终于有人讲明白了
  4. SAP MM Inbound Delivery凭证流里不出现采购订单号?
  5. Google更新最大的带注释图像数据集,添加本地化叙述
  6. 组合求解器 + 深度学习 =?这篇ICLR 2020论文告诉你答案
  7. 【AI学习篇】实战深度学习(3):深度学习的数据表示
  8. CTO多要会刷脸--
  9. 两虎相争将带来优质的互联网搜索服务 --- 我看Google归来!
  10. 《用Python进行自然语言处理》第 1 章 语言处理与 Python