关于cp命令:
1.将一个文件复制到一个目录中,并保持原文件名。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[root@localhost
test
]
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
]
[root@localhost
test
]
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.将一个文件复制到一个目录中,但是重命名这个文件名称。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@localhost
test
]
[root@localhost
test
]
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必须存在。
|
1
2
|
[root@localhost
test
]
cp
: target `dir2' is not a directory
|
|
4.将目录递归复制到目标目录,目标目录可以不存在,因为可以先复制过去后重命名。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@localhost
test
]
[root@localhost
test
]
d1
|-- abcdef
| |-- d1
| | |-- t1
| | |-- t2
| | `-- t3
| |-- d11
| | |-- t1
| | |-- t2
| | `-- t3
| |-- t1
| `-- t22
|-- helloworld
|-- t1
|-- t2
`-- t3
|
|
5.文件有2个方面的内容,一个是文件内容,一个是文件的属性(mode,owner,group,timestamp),考虑下,cp将如何处理文件的属性?
|
1
2
|
在默认情况下,谁复制的这个文件,那么新文件就属于谁的。
可以使用-p(perserve)保持住原来的文件属性。
|
|
6.如果文件是链接文件,那么复制的是链接,还是链接所指向的文件?
|
1
2
|
默认情况下,cp复制的是链接文件指向的文件。
可以通过-P选项,达到只复制链接文件。
|
(有时候,这将非常有用,因为,我们就只是想复制链接而已。)
7.我们常常使用cp -a来实现文件的打包归档存放(保持住文件的一切属性),经常用于备份。
本文转自zfz_linux_boy 51CTO博客,原文链接:http://blog.51cto.com/zhangfengzhe/1406904,如需转载请自行联系原作者