国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Linux > 正文

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

2019-11-02 16:47:48
字體:
來源:轉載
供稿:網友

今天在編寫一個腳本的時候,發現一個比較奇怪的問題:就是在使用cp拷貝當前目錄下所有文件到目標目錄的時候,源和目標目錄大小不同。原來一直沒有留意有這樣的問題,后來查了些資料,才知道以前一直使用的格式有誤,

一、預備

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

cp oldfile newfile

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

cp -r old/ new/

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

[root@dc5 test]# ll new/total 4drwxr-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 20drwxr-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 newdrwxr-xr-x 3 root root 4096 Dec 15 12:14 old./new:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14 .drwxr-xr-x 4 root root 4096 Dec 15 11:55 .../old:total 12drwxr-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 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2./old/test2:total 8drwxr-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 12drwxr-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 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/test2:total 8drwxr-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 12drwxr-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 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/test2:total 8drwxr-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 12drwxr-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 test1drwxr-xr-x 2 root root 4096 Dec 15 12:14 test2new/test2:total 8drwxr-xr-x 2 root root 4096 Dec 15 12:14 .drwxr-xr-x 3 root root 4096 Dec 15 12:25 ..
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湟中县| 大渡口区| 济南市| 泰顺县| 怀宁县| 桂林市| 尼木县| 锡林郭勒盟| 蒙城县| 渑池县| 肇庆市| 高邮市| 景洪市| 永清县| 溧阳市| 焦作市| 贡嘎县| 达日县| 枣阳市| 亚东县| 广西| 庆城县| 林周县| 东平县| 保德县| 白水县| 会同县| 宿松县| 奉节县| 高碑店市| 天全县| 棋牌| 繁昌县| 桂东县| 望谟县| 凌云县| 莱芜市| 大名县| 德庆县| 滁州市| 景宁|