linux cp -w,对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来实现文件的打包归档存放(保持住文件的一切属性),经常用于备份。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值