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年的, ...
随机推荐
- ssh连接卡在【To escape to local shell, press 'Ctrl+Alt+]'.】的解决方法
一.现象 1.使用xshell连接远程主机的时候一直卡在To escape to local shell, press 'Ctrl+Alt+]'.,要等很久才能连上: Connecting to 19 ...
- PowerDesigner生成OOM时类名属性名转换
Examples Script 1: Convert a name into a class code (JAVA naming convention)转换类名 .foreach_part(%Name ...
- C标准库pow函数精度问题。
#include <stdio.h> int main () { int temp,i; double a=2.4568; unsigned ]; ;i<;i++) { temp=( ...
- 微信小程序报Cannot read property 'setData' of undefined的错误
最近在学习微信小程序的开发,让我吐槽的是,都9102年了,怎么还是有有时不能复制,有时不能打中文的bug呢,这个时候,你可以Ctrl+shift+w一下,如果还不行,那就得重启了.. 进入正题吧,刚在 ...
- GITLAB安装笔记
CentOS 7 最小安装后操作 设置时区timedatectl set-timezone Asia/Shanghai 添加 Gitlab 清华源 vi /etc/yum.repos.d/gitlab ...
- socket实现两台FTP服务器指定目录下的文件转移(不依赖第三方jar包)
通过socket实现两台FTP服务器指定目录下的文件转移,其中包含了基础了ftp文件列表显示.上传和下载.这里仅供学习用,需掌握的点有socket.ftp命令.文件流读取转换等 完整代码如下: Ftp ...
- C#递归方法遍历目录及子目录
众所周知,获得某一目录下第一级的所有文件和文件夹列表,很容易办到:DirectoryInfo di=new DirectoryInfo(strBaseDir);//strBaseDir是起始目录,绝对 ...
- SpaceSyntax【空间句法】之DepthMapX学习:第四篇 凸多边形图分析[未完]
这一篇正式讲解分析类型中的第一个,凸多边形分析,流程图参照上一篇的. 博客园/B站/知乎/CSDN @秋意正寒(我觉得这一篇肯定很多盗图的,那么我在版头加个本篇地址吧) https://www.cnb ...
- How to change windows applicatioin's position via Win32 API
可以使用的Win32 API是: [DllImport("user32.dll")] private extern static bool SetWindowPos(IntPtr ...
- 【源码分析】HashMap源码再读-基于Java8
最近工作不是太忙,准备再读读一些源码,想来想去,还是先从JDK的源码读起吧,毕竟很久不去读了,很多东西都生疏了.当然,还是先从炙手可热的HashMap,每次读都会有一些收获.当然,JDK8对HashM ...