描述:複製文件或目錄spa
用法:cp [-adfilprsu] 源文件 目標文件code
cp [option] source1 source2 cource3.... directory遞歸
經常使用選項:io
-a:至關於-pdr test
-d:若源文件爲鏈接文件,則複製鏈接文件屬性而非文件自己;di
-f:爲強制的意思,若目標文件已經存在且沒法開啓,則刪除後再嘗試一次;文件
-i:若目標文件已經存在,在覆蓋時先詢問;備份
-l:進行硬鏈接文件的建立,而非複製文件自己;co
-p:連同文件的屬性一塊兒複製過去,而非使用默認屬性(備份經常使用);background
-r:遞歸持續複製
-s:複製成爲符號連接文件,即快捷方式文件;
-u:若destination比source舊才更新
-v:顯示指令執行過程
示例:1.複製整個目錄
目標目錄存在時:整個源目錄比複製到目標目錄裏面。
[root@share19 ~]# cp -a test1 test2 [root@share19 ~]# ll total 4 -rw-------. 1 root root 1149 Jan 15 10:32 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 May 23 14:44 test1 drwxr-xr-x. 3 root root 18 May 23 14:46 test2 [root@share19 ~]# cd test2 [root@share19 test2]# ll total 0 drwxr-xr-x. 2 root root 6 May 23 14:44 test1 [root@share19 test2]#
目標目錄不存在時:源目錄與目標目錄在一個路徑下。
[root@share19 ~]# ll total 4 -rw-------. 1 root root 1149 Jan 15 10:32 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 May 23 14:44 test1 drwxr-xr-x. 4 root root 30 May 23 14:48 test2 [root@share19 ~]# cp -a test1 test3 [root@share19 ~]# ll total 4 -rw-------. 1 root root 1149 Jan 15 10:32 anaconda-ks.cfg drwxr-xr-x. 2 root root 6 May 23 14:44 test1 drwxr-xr-x. 4 root root 30 May 23 14:48 test2 drwxr-xr-x. 2 root root 6 May 23 14:44 test3
2.複製的 log.log 創建一個連結檔 log_link.log
[root@share19 ~]# cp -s log.log log_link.log [root@share19 ~]# ll total 1 lrwxrwxrwx. 1 root root 7 May 23 14:51 log_link.log -> log.log -rw-r--r--. 1 root root 0 May 23 14:51 log.log