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. ROC,AUC,Precision,Recall,F1的介绍与计算

    1. 基本概念 1.1 ROC与AUC ROC曲线和AUC常被用来评价一个二值分类器(binary classifier)的优劣,ROC曲线称为受试者工作特征曲线 (receiver operatin ...

  2. Nginx 负载均衡和反向代理实践

    nginx 以哪个配置文件启动 Nginx 负载均衡和反向代理实践 环境介绍 192.168.1.50    在这台主机上配置Nginx 的反向代理,负载均衡,和web1,web1使用的81号端口 1 ...

  3. 【文档】使用Sphinx + reST编写文档

    0 前言 写文档是开发人员日常工作中的一项重要内容,除了word之外,我更偏爱使用标记语言(Markup Language).使用标记语言,可以利用简单.免费的文本编辑器(记事本,vim, emacs ...

  4. Redis---skipList(跳跃列表)

    1. 概述 跳跃表是一种有序的数据结构, 他通过在每个节点中维持多个指向其他节点的指针, 从而达到快速访问节点的目的. 大部分情况下, 跳跃表的效率可以和平衡树相媲美. Redis中只在两处用到了跳跃 ...

  5. Spring常用知识点

    说一下spring中Bean的作用域 singleton: Spring IoC容器中只会存在一个共享的Bean实例,无论有多少个Bean引用它,始终指向同一对象.Singleton作用域是Sprin ...

  6. 初识java java的加载与执行(JDK,JVM,JRE关系解释)

    首先java代码是以 .java结尾的文件,通过javac命令编译生成.class编译生成字节码文件,再通过java命令,把字节码文件加载到内存内部,此时是类加载器ClassLoader执行加载,通过 ...

  7. syslog之二:syslog协议及rsyslog服务全解析

    目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...

  8. list、vector、deque互相拷贝

    #include <iostream> #include <stdlib.h> #include <string.h> #include <algorithm ...

  9. CentOS7下搭建FastDfs(V5.11)+Keepalived分布式集群部署

    FastDfs介绍 http://kb.cnblogs.com/page/82280/ 1.准备 系统 CentOS7 最小化安装. #VIP虚拟IP 10.1.6.218 #Keepalived 1 ...

  10. 编译开源C或者C++项目最好像执行如下命令,否则可能会有这种奇葩问题

    find ./ -type f | xargs sed -i 's/\r$//' find ./ -name "*.sh" | xargs chmod +x find ./ -ty ...