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系统VIM编辑器管理(2)

    VI/VIM模式概述 在 Linux 的世界中,绝大部分的配置文件都是以 ASCII 的纯文本形态存在,因此利用简单的文字编辑软件就能够修改设定了,与微软的 Windows 系统不同的是,如果你用惯了 ...

  2. Mac下IDE无法读取环境变量问题

    今天遇到一个问题,Idea无法读取~/.bash_profile下的配置文件. 上网查了好久,都说是launchctl的问题. 但是其实我这边是因为安装了zsh,导致环境标量失效. 在~/.zshrc ...

  3. Yii2框架 数据库常用操作

    通用: use yii\db\Query; $query = new Query(); 查询: Query: $rows = (new \yii\db\Query()) ->select(['c ...

  4. [ActionScript 3.0] as3处理xml的功能和遍历节点

    as3比as2处理xml的功能增强了N倍,获取或遍历节点非常之方便,类似于json对像的处理方式. XML 的一个强大功能是它能够通过文本字符的线性字符串提供复杂的嵌套数据.将数据加载到 XML 对象 ...

  5. mybatis 插件原理

    [传送门]:mybatis 插件原理

  6. centos docker 安装笔记

    安装epel rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm rpm --import ...

  7. python爬取微信公众号

    爬取策略 1.需要安装python selenium模块包,通过selenium中的webdriver驱动浏览器获取Cookie的方法.来达到登录的效果 pip3 install selenium c ...

  8. redo log文件格式描述

  9. 解决org.apache.shiro.session.UnknownSessionException: There is no session with id的问题

    一.背景 最近在整合了Spring+Shiro+Redis实现tomcat集群session共享的问题之后,发布以后运行以后发现老是会出现:org.apache.shiro.session.Unkno ...

  10. discuz 数据库文件密码修改

    网站系统需要修改的位置有两处 Discuz 和 UC-center ①路径:/wwwroot/config/config_global.php 这个根据你网站安装的路径而定. 打开 config_gl ...