码迷,mamicode.com
首页 > 系统相关 > 详细

对Linux的cp命令的思考

时间:2014-05-06 17:37:37      阅读:522      评论:0      收藏:0      [点我收藏+]

标签:linux cp

关于cp命令:

1.将一个文件复制到一个目录中,并保持原文件名。

[root@localhost test]# ls -l
total 44
drwxr-xr-x 2 root         root         4096 May  4 20:56 d1
drwxr-xr-x 4 root         root         4096 May  3 23:00 d2
-rw-r--r-- 1 root         root            0 May  4 20:54 dir1
-rw-rw-r-- 1 zhangfengzhe zhangfengzhe    0 Feb 26  2012 helloworld
-rw-r--r-- 1 root         root            0 May  3 23:05 helloworld2
-rw-rw-r-- 1 zhangfengzhe zhangfengzhe    0 Feb 26  2012 helloworld3
-rw-r--r-- 1 root         root            0 May  3 22:49 t1
-rw-r--r-- 1 root         root            0 May  3 22:49 t2
-rw-r--r-- 1 root         root            0 May  3 22:49 t3
[root@localhost test]# cp /etc/passwd ./
[root@localhost test]# ls -l
total 52
drwxr-xr-x 2 root         root         4096 May  4 20:56 d1
drwxr-xr-x 4 root         root         4096 May  3 23:00 d2
-rw-r--r-- 1 root         root            0 May  4 20:54 dir1
-rw-rw-r-- 1 zhangfengzhe zhangfengzhe    0 Feb 26  2012 helloworld
-rw-r--r-- 1 root         root            0 May  3 23:05 helloworld2
-rw-rw-r-- 1 zhangfengzhe zhangfengzhe    0 Feb 26  2012 helloworld3
-rw-r--r-- 1 root         root         1705 May  4 21:03 passwd
-rw-r--r-- 1 root         root            0 May  3 22:49 t1
-rw-r--r-- 1 root         root            0 May  3 22:49 t2
-rw-r--r-- 1 root         root            0 May  3 22:49 t3
[root@localhost test]#

2.将一个文件复制到一个目录中,但是重命名这个文件名称。

[root@localhost test]# cp /etc/passwd ./mypasswd
[root@localhost test]# ls -l
total 60
drwxr-xr-x 2 root         root         4096 May  4 20:56 d1
drwxr-xr-x 4 root         root         4096 May  3 23:00 d2
-rw-r--r-- 1 root         root            0 May  4 20:54 dir1
-rw-rw-r-- 1 zhangfengzhe zhangfengzhe    0 Feb 26  2012 helloworld
-rw-r--r-- 1 root         root            0 May  3 23:05 helloworld2
-rw-rw-r-- 1 zhangfengzhe zhangfengzhe    0 Feb 26  2012 helloworld3
-rw-r--r-- 1 root         root         1705 May  4 21:05 mypasswd
-rw-r--r-- 1 root         root         1705 May  4 21:03 passwd
-rw-r--r-- 1 root         root            0 May  3 22:49 t1
-rw-r--r-- 1 root         root            0 May  3 22:49 t2
-rw-r--r-- 1 root         root            0 May  3 22:49 t3
[root@localhost test]#

3.将多个文件复制到一个目录中,注意只能是files copy to directory,并且directory必须存在。

[root@localhost test]# cp t1 t2 t3 dir2
cp: target `dir2‘ is not a directory

4.将目录递归复制到目标目录,目标目录可以不存在,因为可以先复制过去后重命名。

[root@localhost test]# cp -r d2 ./d1/abcdef
[root@localhost test]# tree d1
d1
|-- abcdef
|   |-- d1
|   |   |-- t1
|   |   |-- t2
|   |   `-- t3
|   |-- d11
|   |   |-- t1
|   |   |-- t2
|   |   `-- t3
|   |-- t1
|   `-- t22
|-- helloworld
|-- t1
|-- t2
`-- t3

5.文件有2个方面的内容,一个是文件内容,一个是文件的属性(mode,owner,group,timestamp),考虑下,cp将如何处理文件的属性?

在默认情况下,谁复制的这个文件,那么新文件就属于谁的。
可以使用-p(perserve)保持住原来的文件属性。

6.如果文件是链接文件,那么复制的是链接,还是链接所指向的文件?

默认情况下,cp复制的是链接文件指向的文件。
可以通过-P选项,达到只复制链接文件。

(有时候,这将非常有用,因为,我们就只是想复制链接而已。)

7.我们常常使用cp -a来实现文件的打包归档存放(保持住文件的一切属性),经常用于备份。






本文出自 “学习笔记” 博客,请务必保留此出处http://zhangfengzhe.blog.51cto.com/8855103/1406904

对Linux的cp命令的思考,布布扣,bubuko.com

对Linux的cp命令的思考

标签:linux cp

原文地址:http://zhangfengzhe.blog.51cto.com/8855103/1406904

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!