Linux常用基本命令[cp]
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]的更多相关文章
- Linux 常用基本命令及应用技巧
需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...
- Linux常用基本命令(less)
转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...
- 测试必知必会系列- Linux常用命令 - cp
21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 复制文 ...
- Linux常用基本命令[find]用法(1)
find是个很强大的命令,用法很多. 作用:查找目录下的文件,同时也可以调用其他命令执行相应的操作 用法: find [选项] [路径][操作语句] find [-H] [-L] [-P] [-D d ...
- 【Linux】linux常用基本命令(转)
(转自:http://blog.csdn.net/xiaoguaihai/article/details/8705992) Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用 ...
- 【Linux】linux常用基本命令
Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉及到版权问题. 1.显示日 ...
- linux常用基本命令
Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...
- linux常用基本命令整理小结
linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...
- Linux 常用基本命令
这两天有俩哥们问了我linux的事,问我在工作中需不需要用到,需不需要学会 一个是工作1年不到的,我跟他说,建议你学学,在以后肯定是要用到的,虽然用到的机会不多,但是会总比不会好 另一个是工作6年的, ...
随机推荐
- JQuery Mobile - 解决页面点击时候,页眉和页脚消失问题!
当点击页面时候,页眉和页脚会消失!解决方法,在页面和页脚中加入: data-quicklinks="true" 实际使用代码: <div data-role="pa ...
- linux 的计划任务 定时任务
linux的计划任务,也叫做定时任务 https://www.cnblogs.com/mingforyou/p/3930636.html 名字是crond 查看linux本机的定时任务 crontab ...
- redis windows版本下载地址(不用hm提供的)
https://github.com/MicrosoftArchive/redis/releases
- 初探日志框架Logback
一. 背景 最近因为学习项目时需要使用logback日志框架来打印日志, 使用过程中碰到很多的疑惑, 而且需要在控制台打印mybatis执行的sql语句, 于是决定沉下心来 研究一下logback的使 ...
- 记录jquery的ajax
1.直接干货 ajax很简单jquery有很好的支持,原生js就不写了.总的说常用的有3个方法 $.post $.get $.ajax 具体参数参考教程http://www.runoob.com/jq ...
- [LeetCode]640解方程式
问题描述: 示例 1: 输入: "x+5-3+x=6+x-2" 输出: "x=2" 示例 2: 输入: "x=x" 输出: "In ...
- 找到IIS 站点对应的站点日志
IIS6 下 IIS7 下 1 找到日志文件的路径 2 找到站点ID 3 打开日志文件路径,找到站点ID 对应的日志文件夹.文件夹的最后一位数字,就对应着站点ID.
- Intellij添加Jetty远程Debug
步骤一: 步骤二: 步骤三:-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=15005 步骤四: 找到服务器上jetty的b ...
- 选择排序:直接选择排序&堆排序
上一篇中, 介绍了交换排序中的冒泡排序和快速排序, 那么这一篇就来介绍一下 选择排序和堆排序, 以及他们与快速排序的比较. 一.直接选择排序 1. 思想 在描述直接选择排序思想之前, 先来一个假设吧. ...
- 「每日一码」a&b赋值问题
每日一码 将每天看到的优秀的代码或者特别的实现,记录下来 a&b赋值问题 2019-2-18 var a = {n: 1}; var b = a; a.x = a = {n: 2}; Q&am ...