Linux中拷贝 cp命令中拷贝所有的写法详解

2019-10-13 10:58:51王振洲

不用*号,而用.号代替。

还有一种比较复杂一些的写法:

[root@dc5 test]# cp -a old/* old/.[^.]* new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x 3 root root 4096 Dec 15 12:25 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:07 .test3
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

new/test2:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 3 root root 4096 Dec 15 12:25 ..

请注意写法,不要写成.*了。(原因请看下面)

三、问题2

上面提到不要写成.,那.代表什么?

[root@dc5 test]# echo .*
. ..

.*代表的是当前目录,以及上一层目录。

所以,使用.*会导致更大的问题:

[root@dc5 test]# cp -a old/.* new/
cp: cannot copy a directory, `old/..', into itself, `new/'
cp: cannot copy a directory, `old/..', into itself, `new/'
cp: will not create hard link `new/old' to directory `new/.'
cp: overwrite `new/.test3'? y
[root@dc5 test]# ll -laR new/
new/:
total 16
drwxr-xr-x 4 root root 4096 Dec 15 11:55 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:07 .test3
drwxr-xr-x 2 root root 4096 Dec 15 12:14 new
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

new/new:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-rw-r--r-- 1 root root  0 Dec 15 12:07 .test3
-rw-r--r-- 1 root root  0 Dec 15 12:05 test1

new/test2:
total 8
drwxr-xr-x 2 root root 4096 Dec 15 12:14 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..

也就是说,使用.*就等于这样了:

[root@dc5 test]# cp -a old/. old/.. old/.test3 new/
[root@dc5 test]# echo old/.*
old/. old/.. old/.test3

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。