日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux命令中的cp,Linux高级技术:关于cp命令中拷贝所有的写法

發布時間:2025/3/12 linux 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux命令中的cp,Linux高级技术:关于cp命令中拷贝所有的写法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux高級技術:關于cp命令中拷貝所有的寫法

發布時間:2007-09-30 21:39:36來源:紅聯作者:GiftPot

今天在編寫一個腳本的時候,發現一個比較奇怪的問題:就是在使用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

四、擴展

其實這樣的問題,不單cp命令有這樣的問題,在所有涉及含有特殊字符意義文件的命令時,都需要考慮,例如rm:

引用

[root@dc5 new]# ll -a

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

[root@dc5 new]# rm -rf *

[root@dc5 new]# ll -a

total 8

drwxr-xr-x 2 root root 4096 Dec 15 12:40 .

drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..

-rw-r--r-- 1 root root 0 Dec 15 12:07 .test3

正確的寫法應該是:

引用

[root@dc5 new]# rm -rf .* *

rm: cannot remove `.' or `..'

rm: cannot remove `.' or `..'

[root@dc5 new]# ll -a

total 8

drwxr-xr-x 2 root root 4096 Dec 15 12:42 .

drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..

當然,這是一樣的:

引用

[root@dc5 new]# rm -rf * .[^.]*

[root@dc5 new]# ll -a

total 8

drwxr-xr-x 2 root root 4096 Dec 15 12:44 .

drwxr-xr-x 4 root root 4096 Dec 15 11:55 ..

很多時候,預計的和實際的結果是完全不一樣的。bash編寫腳本尤其需要注意。

總結

以上是生活随笔為你收集整理的linux命令中的cp,Linux高级技术:关于cp命令中拷贝所有的写法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。