cp:复制文件或者目录
用法格式:
cp [option] [source] [dest]
cp [选项] [源文件] [目标文件]
>用root账户,创建文件,复制文件
root@dev:/home/ghostwu/linux/cp# vim 1.txt root@dev:/home/ghostwu/linux/cp# ls -l total 4 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt root@dev:/home/ghostwu/linux/cp# cp 1.txt 2.txt root@dev:/home/ghostwu/linux/cp# ls -l total 8 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--r-- 1 root root 19 5月 6 17:48 2.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 8 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--r-- 1 root root 19 5月 6 17:48 2.txt ghostwu@dev:~/linux/cp$ cp 2.txt 3.txt cp: cannot create regular file '3.txt': Permission denied
上面,当我切换到ghostwu这个账户去复制的时候,权限不允许,因为2.txt这个文件的其他组只有只读权限,而cp需要写权限,所以就报了一个无权限创建复制的文件。
方法一,用sudo提权
ghostwu@dev:~/linux/cp$ ls -l total 8 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--r-- 1 root root 19 5月 6 17:48 2.txt ghostwu@dev:~/linux/cp$ sudo cp 2.txt 3.txt [sudo] password for ghostwu: ghostwu@dev:~/linux/cp$ ls -l total 12 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--r-- 1 root root 19 5月 6 17:48 2.txt -rw-r--r-- 1 root root 19 5月 6 17:52 3.txt
方法二,用root用户给文件的其他组用户可写权限,同时普通用户要对文件所属的目录拥有写权限,也就是要对 "cp" 这个目录拥有写权限
ghostwu@dev:~/linux$ ls -l total 4 drwxr-xr-x 2 root root 4096 5月 6 17:52 cp ghostwu@dev:~/linux$ sudo chmod o+w cp ghostwu@dev:~/linux$ ls -l total 4 drwxr-xrwx 2 root root 4096 5月 6 17:52 cp ghostwu@dev:~/linux$ cd cp ghostwu@dev:~/linux/cp$ ls -l total 12 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--r-- 1 root root 19 5月 6 17:48 2.txt -rw-r--rw- 1 root root 19 5月 6 17:52 3.txt ghostwu@dev:~/linux/cp$ sudo chmod o+w 2.txt ghostwu@dev:~/linux/cp$ ls -l total 12 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--rw- 1 root root 19 5月 6 17:48 2.txt -rw-r--rw- 1 root root 19 5月 6 17:52 3.txt ghostwu@dev:~/linux/cp$ cp 2.txt 4.txt ghostwu@dev:~/linux/cp$ ls -l total 16 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--rw- 1 root root 19 5月 6 17:48 2.txt -rw-r--rw- 1 root root 19 5月 6 17:52 3.txt -rw-r--r-- 1 ghostwu ghostwu 19 5月 6 17:58 4.txt
用普通用户去复制root账户创建的2.txt文件,起一个新名字4.txt,默认情况下cp 改变了文件的权限和时间属性,如果在复制的时候想保留文件原有的权限信息以及时间属性时,可以加参数 -p
ghostwu@dev:~/linux/cp$ ls -l total 16 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--rw- 1 root root 19 5月 6 17:48 2.txt -rw-r--rw- 1 root root 19 5月 6 17:52 3.txt -rw-r--r-- 1 ghostwu ghostwu 19 5月 6 17:58 4.txt ghostwu@dev:~/linux/cp$ cp -p 2.txt 5.txt ghostwu@dev:~/linux/cp$ ls -l total 20 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--rw- 1 root root 19 5月 6 17:48 2.txt -rw-r--rw- 1 root root 19 5月 6 17:52 3.txt -rw-r--r-- 1 ghostwu ghostwu 19 5月 6 17:58 4.txt -rw-r--rw- 1 ghostwu ghostwu 19 5月 6 17:48 5.txt
-i: 带提示信息的复制,默认情况下,cp命令会直接覆盖
ghostwu@dev:~/linux/cp$ ls -l total 20 -rw-r--r-- 1 root root 19 5月 6 17:47 1.txt -rw-r--rw- 1 root root 19 5月 6 17:48 2.txt -rw-r--rw- 1 root root 19 5月 6 17:52 3.txt -rw-r--r-- 1 ghostwu ghostwu 19 5月 6 17:58 4.txt -rw-r--rw- 1 ghostwu ghostwu 19 5月 6 17:48 5.txt ghostwu@dev:~/linux/cp$ cp 2.txt 5.txt ghostwu@dev:~/linux/cp$ cp -i 2.txt 5.txt cp: overwrite '5.txt'? y
-r参数:递归复制目录以及文件
ghostwu@dev:~/linux/cp$ ls -l
total 20
-rw-r--r-- 1 root root 19 5月 6 17:47 1.txt
-rw-r--rw- 1 root root 19 5月 6 17:48 2.txt
-rw-r--rw- 1 root root 19 5月 6 17:52 3.txt
-rw-r--r-- 1 ghostwu ghostwu 19 5月 6 17:58 4.txt
-rw-r--rw- 1 ghostwu ghostwu 19 5月 6 18:04 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
├── 1.txt
├── 2.txt
├── 3.txt
├── 4.txt
└── 5.txt
2 directories, 5 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
│ ├── 1.txt
│ ├── 2.txt
│ ├── 3.txt
│ ├── 4.txt
│ └── 5.txt
└── a2
└── b
├── 1.txt
├── 2.txt
├── 3.txt
├── 4.txt
└── 5.txt
4 directories, 10 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