1. 程式人生 > >Linux中拷貝 cp命令中拷貝所有的寫法詳解

Linux中拷貝 cp命令中拷貝所有的寫法詳解

一、預備 

cp就是拷貝,最簡單的使用方式就是:

cp oldfile newfile

但這樣只能拷貝檔案,不能拷貝目錄,所以通常用:

cp -r old/ new/

那就會把old目錄整個拷貝到new目錄下。注意,不是把old目錄裡面的檔案拷貝到new目錄,而是把old直接拷貝到new下面,結果是:

[root@dc5 test]# ll new/
total 4
drwxr-xr-x 2 root root 4096 Dec 15 11:55 old

那如果要保持原始檔的所有許可權,可以這樣:

cp -rp old/ new/

-p引數,可以保持許可權、宿主、時間棧,還可能包括link等;還有更簡單的,就是用:

cp -a old/new/

-a引數,就等於-dpR。

二、問題1 

好,我們來看看這次的問題。環境是: 

◎兩個目錄:old、new,其中old裡面有個三個內容:test1檔案、test2目錄,還有就是.test3,這是一個隱含檔案。

[root@dc5 test]# ll -laR
.:
total 20
drwxr-xr-x 4 root root 4096 Dec 15 11:55 .
drwxrwxrwt 7 root root 4096 Dec 15 11:59 ..
drwxr-xr-x 2 root root 4096 Dec 15 12:14 new
drwxr-xr-x 3 root root 4096 Dec 15 12:14 old

./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 ..

./old:
total 12
drwxr-xr-x 3 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
drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2

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

◎操作一:

[root@dc5 test]# cp -a old/* new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x 3 root root 4096 Dec 15 12:15 .
drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..
-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:15 ..

問題出來了:隱含的.test3檔案沒有一齊拷貝到new目錄下。 

原因是:引數使用不正確。這樣的寫法,通常都是因為熟悉了過去Dos的格式(包括我自己),而實際在bash環境下,cp使用是不能匹配類似.開頭的隱含檔案的。 

◎操作二 

正確的寫法應該這樣:

[root@dc5 test]# cp -a old/. new/
[root@dc5 test]# ll -laR new/
new/:
total 12
drwxr-xr-x 3 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
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:14 ..

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

還有一種比較複雜一些的寫法:

[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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援指令碼之家。