每天一個linux命令(8):cp命令

來源:互聯網
上載者:User


每天一個linux命令(8):cp命令 相關連結:每天一個linux命令(1):ls命令http://www.bkjia.com/os/201210/163049.html;每天一個linux命令(2):cd命令http://www.bkjia.com/os/201210/163050.html;每天一個linux命令(3):pwd命令http://www.bkjia.com/os/201210/163462.html;每天一個linux命令(4):mkdir命令http://www.bkjia.com/os/201210/163463.html;每天一個linux命令(5):rm命令http://www.bkjia.com/os/201210/163662.html;每天一個linux命令(6):rmdir命令http://www.bkjia.com/os/201210/164017.html;每天一個linux命令(7):mv命令http://www.bkjia.com/os/201210/164247.html cp命令用來複製檔案或者目錄,是Linux系統中最常用的命令之一。一般情況下,shell會設定一個別名,在命令列下複製檔案時,如果目標檔案已經存在,就會詢問是否覆蓋,不管你是否使用-i參數。但是如果是在shell指令碼中執行cp時,沒有-i參數時不會詢問是否覆蓋。這說明命令列和shell指令碼的執行方式有些不同。 1.命令格式:  www.2cto.com  用法:  cp [選項]... [-T] 源 目的      或:cp [選項]... 源... 目錄       或:cp [選項]... -t 目錄 源...2.命令功能:將源檔案複製至目標檔案,或將多個源檔案複製至目標目錄。  www.2cto.com  3.命令參數:-a, --archive    等於-dR --preserve=all    --backup[=CONTROL    為每個已存在的目標檔案建立備份-b                類似--backup 但不接受參數   --copy-contents        在遞迴處理是複製特殊檔案內容-d                等於--no-dereference --preserve=links-f, --force        如果目標檔案無法開啟則將其移除並重試(當 -n 選項                    存在時則不需再選此項)-i, --interactive        覆蓋前詢問(使前面的 -n 選項失效)-H                跟隨源檔案中的命令列符號連結-l, --link            連結檔案而不複製-L, --dereference   總是跟隨符號連結-n, --no-clobber   不要覆蓋已存在的檔案(使前面的 -i 選項失效)-P, --no-dereference   不跟隨源檔案中的符號連結-p                等於--preserve=模式,所有權,時間戳記    --preserve[=屬性列表   保持指定的屬性(預設:模式,所有權,時間戳記),如果               可能保持附加屬性:環境、連結、xattr 等-R, -r, --recursive  複製目錄及目錄內的所有項目  www.2cto.com  4.命令執行個體:執行個體一:複製單個檔案到目標目錄,檔案在目標檔案中不存在命令:cp log.log test5輸出:[root@localhost test]# cp log.log test5[root@localhost test]# ll-rw-r--r-- 1 root root    0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxr-xr-x 2 root root 4096 10-28 14:53 test5[root@localhost test]# cd test5[root@localhost test5]# ll-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log-rw-r--r-- 1 root root 0 10-28 14:53 log.log說明:在沒有帶-a參數時,兩個檔案的時間是不一樣的。在帶了-a參數時,兩個檔案的時間是一致的。    www.2cto.com  執行個體二:目標檔案存在時,會詢問是否覆蓋命令:cp log.log test5輸出:[root@localhost test]# cp log.log test5cp:是否覆蓋“test5/log.log”? n[root@localhost test]# cp -a log.log test5cp:是否覆蓋“test5/log.log”? y[root@localhost test]# cd test5/[root@localhost test5]# ll-rw-r--r-- 1 root root 0 10-28 14:46 log5-1.log-rw-r--r-- 1 root root 0 10-28 14:46 log5-2.log-rw-r--r-- 1 root root 0 10-28 14:46 log5-3.log-rw-r--r-- 1 root root 0 10-28 14:48 log.log說明:目標檔案存在時,會詢問是否覆蓋。這是因為cp是cp -i的別名。目標檔案存在時,即使加了-f標誌,也還會詢問是否覆蓋。  www.2cto.com  執行個體三:複製整個目錄命令:輸出:目標目錄存在時:[root@localhost test]# cp -a test3 test5 [root@localhost test]# ll-rw-r--r-- 1 root root    0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxr-xr-x 3 root root 4096 10-28 15:11 test5[root@localhost test]# cd test5/[root@localhost test5]# ll-rw-r--r-- 1 root root    0 10-28 14:46 log5-1.log-rw-r--r-- 1 root root    0 10-28 14:46 log5-2.log-rw-r--r-- 1 root root    0 10-28 14:46 log5-3.log-rw-r--r-- 1 root root    0 10-28 14:48 log.logdrwxrwxrwx 2 root root 4096 10-28 14:47 test3 目標目錄不存在是:[root@localhost test]# cp -a test3 test4[root@localhost test]# ll-rw-r--r-- 1 root root    0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxrwxrwx 2 root root 4096 10-28 14:47 test4drwxr-xr-x 3 root root 4096 10-28 15:11 test5[root@localhost test]#說明:注意目標目錄存在與否結果是不一樣的。目標目錄存在時,整個來源目錄被複製到目標目錄裡面。   www.2cto.com  執行個體四:複製的 log.log 建立一個連結檔 log_link.log命令:cp -s log.log log_link.log輸出:[root@localhost test]# cp -s log.log log_link.log[root@localhost test]# lllrwxrwxrwx 1 root root    7 10-28 15:18 log_link.log -> log.log-rw-r--r-- 1 root root    0 10-28 14:48 log.logdrwxr-xr-x 6 root root 4096 10-27 01:58 scfdrwxrwxrwx 2 root root 4096 10-28 14:47 test3drwxrwxrwx 2 root root 4096 10-28 14:47 test4drwxr-xr-x 3 root root 4096 10-28 15:11 test5說明:那個 log_link.log 是由 -s 的參數造成的,建立的是一個『捷徑』,所以您會看到在檔案的最右邊,會顯示這個檔案是『連結』到哪裡去的! 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.