linux 拷贝隐藏文件,CENTOS中星号(*)拷贝和移动无法通配隐藏文件

在CentOS中,如果在拷贝(cp)或移动(mv)文件时使用了星号(*),则所指定通配起始目录下的隐藏文件将无法被通配失效,如有以下目录:

[root@test source]# pwd

/root/source

通过ls -l命令查看非隐藏文件:

[root@test source]# ls -l

total 0

-rw-r--r--. 1 root root 0 Jan 18 14:52 textfile01

-rw-r--r--. 1 root root 0 Jan 18 14:52 textfile02

在用ls -la命令查看所有文件(包含隐藏文件):

[root@test source]# ls -al

total 8

drwxr-xr-x. 2 root root 4096 Jan 18 14:52 .

dr-xr-x---. 5 root root 4096 Jan 18 14:51 ..

-rw-r--r--. 1 root root    0 Jan 18 14:52 .hiddenfile

-rw-r--r--. 1 root root    0 Jan 18 14:52 .htaccess

-rw-r--r--. 1 root root    0 Jan 18 14:52 textfile01

-rw-r--r--. 1 root root    0 Jan 18 14:52 textfile02

其中.hiddenfile和.htaccess为隐藏文件(实际上.和..也是隐藏文件)。我们先创建一个目标目录,用于存放拷贝内容:

[root@test source]# cd ..

[root@test ~]# mkdir target

[root@test ~]# ls -l

total 8

drwxr-xr-x. 2 root root 4096 Jan 18 14:52 source

drwxr-xr-x. 2 root root 4096 Jan 18 14:57 target

然后开始用星号(*)尝试拷贝:

[root@test ~]# cp -rf source/* target/

[root@test ~]# ls -al target/

total 8

drwxr-xr-x. 2 root root 4096 Jan 18 14:59 .

dr-xr-x---. 4 root root 4096 Jan 18 14:57 ..

-rw-r--r--. 1 root root    0 Jan 18 14:59 textfile01

-rw-r--r--. 1 root root    0 Jan 18 14:59 textfile02

可见,拷贝失败了。

两个解决思路:

1.在星号前加”.”,以显式地声明需要通配的是隐藏文件。但这种方法需要人为地过滤掉’.'和’..’,否则就是死循环。

2.不用星号(*)通配,而直接选定目录作为拷贝目标,在上例中为:

[root@test ~]# cp -rf source/ target

[root@test ~]# ls -al target/

total 8

drwxr-xr-x. 2 root root 4096 Jan 18 15:04 .

dr-xr-x---. 4 root root 4096 Jan 18 15:04 ..

-rw-r--r--. 1 root root    0 Jan 18 15:04 .hiddenfile

-rw-r--r--. 1 root root    0 Jan 18 15:04 .htaccess

-rw-r--r--. 1 root root    0 Jan 18 15:04 textfile01

-rw-r--r--. 1 root root    0 Jan 18 15:04 textfile02

#注意cp的目标是target和不是target/,读者可以尝试两者的不同。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值