格式:
-
cp [-adfilprsu] 来源文件(source) 目标文件(destination)
-
cp [options] source1 source2 source3 .... directory(复制多个文件到同意目录下)
对于我等萌新来说 东西最好是简单 ,易懂 。太多的选择反而会起到干扰的作用 所以 我只会列出几个常用的参数。
- -a :相当于 -dr --preserve=all 的意思,至于 dr 请参考下列说明;(常用)
- -d :若来源文件为链接文件的属性(link file),则复制链接文件属性而非文件本身
- -i :若目标文件(destination)已经存在时,在覆盖时会先询问动作的进行(常用)
- -p :连同文件的属性(权限、用户、时间)一起复制过去,而非使用默认属性(备份常用)
- -r :递归持续复制,用于目录的复制行为(常用)
其中 -a -p 命令是非常认主人的 不同的身份执行这两个参数会有很大的差别。
实例
我们就以 我电脑上的 holle.c 为例 展示 cp 的强大魅力。
- -a参数 以root账户为例
BORBER# ls -l
total 20
-rwxr-xr-x 1 x x 8368 Jun 26 13:36 hello
-rw-r--r-- 1 root root 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4096 Jun 26 16:25 test1
BORBER# cp -a hello.c ./test1
BORBER# ls
hello hello.c test1
BORBER# cd test1
BORBER# ls
hello.c
BORBER# ls -l
total 4.0K
-rw-r--rwx 1 root root 99 Jun 26 13:34 hello.c
BORBER#
hello.c 的 拥有者 所属组 都是root 我们可以看到 复制后的 hello.c 和原来的基本一模一样
- -a参数 以普通账户为例
Downloads/tmp/test
▶ ll
total 20K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--r-- 1 root root 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4.0K Jun 26 16:35 test1
Downloads/tmp/test
▶ cp -a hello.c ./test1
Downloads/tmp/test
▶ cd test1
tmp/test/test1
▶ ll
total 0
tmp/test/test1
▶
没有报错 但我们并没有成功。我们给 x 加上 hello 的 写 和执行命令 再试试。
Downloads/tmp/test
▶ sudo chmod o+wx hello.c
[sudo] password for x:
Downloads/tmp/test
▶ ll
total 24K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--rwx 1 root root 99 Jun 26 13:34 hello.c
-rw-r--r-- 1 x x 99 Jun 26 13:34 teat1
drwxr-xr-x 2 x x 4.0K Jun 26 16:35 test1
Downloads/tmp/test
▶ cp -a hello.c ./test1
Downloads/tmp/test
▶ cd test1
tmp/test/test1
▶ ll
total 4.0K
-rw-r--rwx 1 x x 99 Jun 26 13:34 hello.c
tmp/test/test1
▶
你可以看到 我们成功了 但是 细心的你 有没有发现 复制之后的 hello.c 的 拥有者和 所属组都变了 成了 x
这就是我要说的了 由此 我有一个 奇妙的想法。
Downloads/tmp/test
▶ ll
total 20K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--rw- 1 root root 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4.0K Jun 26 16:50 test1
Downloads/tmp/test
▶ chmod o+x hello.c
chmod: changing permissions of 'hello.c': Operation not permitted
Downloads/tmp/test ⍉
▶ cp -a hello.c x.c
Downloads/tmp/test
▶ ll
total 24K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--rw- 1 root root 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4.0K Jun 26 16:50 test1
-rw-r--rw- 1 x x 99 Jun 26 13:34 x.c
Downloads/tmp/test
▶ chmod o+x x.c
Downloads/tmp/test
▶ ll
total 24K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--rw- 1 root root 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4.0K Jun 26 16:50 test1
-rw-r--rwx 1 x x 99 Jun 26 13:34 x.c
Downloads/tmp/test
▶ rm hello.c
Downloads/tmp/test
▶ ll
total 20K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
drwxr-xr-x 2 x x 4.0K Jun 26 16:50 test1
-rw-r--rwx 1 x x 99 Jun 26 13:34 x.c
Downloads/tmp/test
▶ mv x.c hello.c
Downloads/tmp/test
▶ ll
total 20K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--rwx 1 x x 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4.0K Jun 26 16:50 test1
Downloads/tmp/test
▶
我们通过 复制 修改 成功的把 root 用户的东西 变成了自己的 并加上了 执行权限 。哈哈哈。你可以看到之前 直接修改是失败了的。 所以说 身份对于 cp 命令是很重要的
-
-p 则是 将其 完整的信息 全部复制 连修改时间都是如此 所以 用来备份 。在此不做演示。同 -a一样 这也是一个认生的命令 , 不同的身份使用 结果不同。
-
-r 可以类比 rm -rf /* (这个命令非常危险 千万不可尝试) -r 可以把 文件夹复制 。
4.-i 在此做一个简单的示范
Downloads/tmp/test
▶ ll
total 20K
-rwxr-xr-x 1 x x 8.2K Jun 26 13:36 hello
-rw-r--rwx 1 x x 99 Jun 26 13:34 hello.c
drwxr-xr-x 2 x x 4.0K Jun 26 16:50 test1
Downloads/tmp/test
▶ cp -i hello.c hello.c
cp: 'hello.c' and 'hello.c' are the same file
Downloads/tmp/test ⍉
▶
文件已存在会有一个警告。
这就是 cp 命令的不完整版萌新入门介绍 ,希望我的学习笔记对你有所帮助。
边鼓 于2018/6/26
##################################################################################
最后附上我比较喜欢的桌面:
海
网友评论