cp命令用來復(fù)制文件或者目錄,是linux系統(tǒng)中最常用的命令之一。一般情況下,shell會(huì)設(shè)置一個(gè)別名,在命令行下復(fù)制文件時(shí),如果目標(biāo)文件已經(jīng)存在,就會(huì)詢問是否覆蓋,不管你是否使用-i參數(shù)。但是如果是在shell腳本中執(zhí)行cp時(shí),沒有-i參數(shù)時(shí)不會(huì)詢問是否覆蓋。這說明命令行和shell腳本的執(zhí)行方式有些不同。
1.命令格式: 用法:
cp [選項(xiàng)]... [-T] 源 目的或:cp [選項(xiàng)]... 源... 目錄或:cp [選項(xiàng)]... -t 目錄 源...2.命令功能:
將源文件復(fù)制至目標(biāo)文件,或?qū)⒍鄠€(gè)源文件復(fù)制至目標(biāo)目錄。
3.命令參數(shù):
-a, –archive 等于-dR –PReserve=all–backup[=CONTROL 為每個(gè)已存在的目標(biāo)文件創(chuàng)建-b 類似–backup 但不接受參數(shù)–copy-contents 在遞歸處理是復(fù)制特殊文件內(nèi)容-d 等于–no-dereference –preserve=links-f, –force 如果目標(biāo)文件無法打開則將其移除并重試(當(dāng) -n 選項(xiàng)存在時(shí)則不需再選此項(xiàng))-i, –interactive 覆蓋前詢問(使前面的 -n 選項(xiàng)失效)-H 跟隨源文件中的命令行符號(hào)鏈接-l, –link 鏈接文件而不復(fù)制-L, –dereference 總是跟隨符號(hào)鏈接-n, –no-clobber 不要覆蓋已存在的文件(使前面的 -i 選項(xiàng)失效)-P, –no-dereference 不跟隨源文件中的符號(hào)鏈接-p 等于–preserve=模式,所有權(quán),時(shí)間戳–preserve[=屬性列表 保持指定的屬性(默認(rèn):模式,所有權(quán),時(shí)間戳),如果可能保持附加屬性:環(huán)境、鏈接、xattr 等-R, -r, –recursive 復(fù)制目錄及目錄內(nèi)的所有項(xiàng)目4.命令實(shí)例:
實(shí)例一:復(fù)制單個(gè)文件到目標(biāo)目錄,文件在目標(biāo)文件中不存在 命令:
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參數(shù)時(shí),兩個(gè)文件的時(shí)間是不一樣的。在帶了-a參數(shù)時(shí),兩個(gè)文件的時(shí)間是一致的。
實(shí)例二:目標(biāo)文件存在時(shí),會(huì)詢問是否覆蓋 命令:
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說明:
目標(biāo)文件存在時(shí),會(huì)詢問是否覆蓋。這是因?yàn)閏p是cp -i的別名。目標(biāo)文件存在時(shí),即使加了-f標(biāo)志,也還會(huì)詢問是否覆蓋。
實(shí)例三:復(fù)制整個(gè)目錄 命令:
cp -a test1 test2輸出: 目標(biāo)目錄存在時(shí):
[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目標(biāo)目錄不存在是:
[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]#說明:
注意目標(biāo)目錄存在與否結(jié)果是不一樣的。目標(biāo)目錄存在時(shí),整個(gè)源目錄被復(fù)制到目標(biāo)目錄里面。
實(shí)例四:復(fù)制的 log.log 建立一個(gè)連結(jié)檔 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說明:
那個(gè) log_link.log 是由 -s 的參數(shù)造成的,建立的是一個(gè)『快捷方式』,所以您會(huì)看到在文件的最右邊,會(huì)顯示這個(gè)文件是『連結(jié)』到哪里去的!
新聞熱點(diǎn)
疑難解答
圖片精選