cp:复制文件或者目录

用法格式:

cp [option] [source] [dest]

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

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

root@dev:/home/ghostwu/linux/cp# vim .txt
root@dev:/home/ghostwu/linux/cp# ls -l
total
-rw-r--r-- root root 5月 : .txt
root@dev:/home/ghostwu/linux/cp# cp .txt .txt
root@dev:/home/ghostwu/linux/cp# ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--r-- root root 5月 : .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
-rw-r--r-- root root 5月 : .txt
-rw-r--r-- root root 5月 : .txt
ghostwu@dev:~/linux/cp$ cp .txt .txt
cp: cannot create regular file '3.txt': Permission denied

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

方法一,用sudo提权

ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--r-- root root 5月 : .txt
ghostwu@dev:~/linux/cp$ sudo cp .txt .txt
[sudo] password for ghostwu:
ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--r-- root root 5月 : .txt
-rw-r--r-- root root 5月 : .txt

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

ghostwu@dev:~/linux$ ls -l
total
drwxr-xr-x root root 5月 : cp
ghostwu@dev:~/linux$ sudo chmod o+w cp
ghostwu@dev:~/linux$ ls -l
total
drwxr-xrwx root root 5月 : cp
ghostwu@dev:~/linux$ cd cp
ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
ghostwu@dev:~/linux/cp$ sudo chmod o+w .txt
ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
ghostwu@dev:~/linux/cp$ cp .txt .txt
ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--r-- ghostwu ghostwu 5月 : .txt

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

ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--r-- ghostwu ghostwu 5月 : .txt
ghostwu@dev:~/linux/cp$ cp -p .txt .txt
ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--r-- ghostwu ghostwu 5月 : .txt
-rw-r--rw- ghostwu ghostwu 5月 : .txt

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

ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--r-- ghostwu ghostwu 5月 : .txt
-rw-r--rw- ghostwu ghostwu 5月 : .txt
ghostwu@dev:~/linux/cp$ cp .txt .txt
ghostwu@dev:~/linux/cp$ cp -i .txt .txt
cp: overwrite '5.txt'? y

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

ghostwu@dev:~/linux/cp$ ls -l
total
-rw-r--r-- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--rw- root root 5月 : .txt
-rw-r--r-- ghostwu ghostwu 5月 : .txt
-rw-r--rw- ghostwu ghostwu 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
├── .txt
├── .txt
├── .txt
├── .txt
└── .txt directories, 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
│   ├── .txt
│   ├── .txt
│   ├── .txt
│   ├── .txt
│   └── .txt
└── a2
└── b
├── .txt
├── .txt
├── .txt
├── .txt
└── .txt directories, 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 .txt
ghostwu@dev:~/linux/cp$ cp .txt .txt
ghostwu@dev:~/linux/cp$ cp .txt .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
.txt .txt a a2
ghostwu@dev:~/linux/cp$ cp .txt .txt
cp: overwrite '2.txt'? y
ghostwu@dev:~/linux/cp$ which cp
/bin/cp
ghostwu@dev:~/linux/cp$ /bin/cp .txt .txt

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

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

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

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

Linux常用基本命令[cp]的更多相关文章

  1. Linux 常用基本命令及应用技巧

    需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...

  2. Linux常用基本命令(less)

    转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...

  3. 测试必知必会系列- Linux常用命令 - cp

    21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 复制文 ...

  4. Linux常用基本命令[find]用法(1)

    find是个很强大的命令,用法很多. 作用:查找目录下的文件,同时也可以调用其他命令执行相应的操作 用法: find [选项] [路径][操作语句] find [-H] [-L] [-P] [-D d ...

  5. 【Linux】linux常用基本命令(转)

    (转自:http://blog.csdn.net/xiaoguaihai/article/details/8705992) Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用 ...

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

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日 ...

  7. linux常用基本命令

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...

  8. linux常用基本命令整理小结

    linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...

  9. Linux 常用基本命令

    这两天有俩哥们问了我linux的事,问我在工作中需不需要用到,需不需要学会 一个是工作1年不到的,我跟他说,建议你学学,在以后肯定是要用到的,虽然用到的机会不多,但是会总比不会好 另一个是工作6年的, ...

随机推荐

  1. Linux高级文件系统管理(8)

    如果您的 Linux 服务器有多个用户经常存取数据时,为了维护所有使用者在硬盘容量的公平使用,磁碟配额 (Quota) 就是一项非常有用的工具,另外,如果你的用户常常抱怨磁盘容量不够用,那么更进阶的文 ...

  2. Python 生成器的使用(yield)

    一. 生成器就是一个特殊的迭代器, 使用关键字yield就可以生成一个生成器 def func(): for i in range(10): yield i item = func() yield i ...

  3. 利用Python做绝地科学家(外挂篇)

    i春秋作家:奶权 前言  玩吃鸡时间长的鸡友们 应该都知道现在的游戏环境非常差 特别在高端局 神仙满天飞 搞得很多普通玩家非常没有游戏体验  因为吃鸡的火爆 衍生出了一条巨大的外挂利益链 导致市面上出 ...

  4. Spring Boot log4j多环境日志级别的控制

    之前介绍了在<Spring boot中使用log4j>,仅通过log4j.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需要采用D ...

  5. UICollectionView设置首个cell默认选中(二)

    上篇对于UICollectionView默认选中cell采取的是每个cell分别对应一个标识,也就代表着废除了UICollectionView的重用机制.对于较少的数据情况是可以的,但是对于数据比较大 ...

  6. eclipse上搭建mybatis

    1..在help中打开 2.搜索mybatipse 3:功能简介 1:要查找某一个方法        在dao接口中某一个方法中 按住 Ctrl键 鼠标指到方法名称上 选择open xml 就会自动跳 ...

  7. docker部署consol 集群

    拉取镜像 docker pull consul 启动节点1 docker run -d -e 'CONSUL_LOCAL_CONFIG={"skip_leave_on_interrupt&q ...

  8. vue教程2-04 vue实例简单方法

    vue教程2-04 vue实例简单方法 vue实例简单方法: vm.$el -> 就是元素 vm.$data -> 就是data <!DOCTYPE html> <htm ...

  9. copy代码的时候,如何去掉代码前边的编号

    从网页上拷贝下来的代码前面总有编号,如何去掉! 1.使用正则表达式:在editorplus(notepad++)里按ctrl+h,弹出框里勾选上“正则表达式(regular expression)”, ...

  10. 剑指offer十四之链表中倒数第k个结点

    一.题目 输入一个链表,输出该链表中倒数第k个结点. 二.思路 两个指针,先让第一个指针和第二个指针都指向头结点,然后再让第一个指正走(k-1)步,到达第k个节点.然后两个指针同时往后移动,当第一个结 ...