溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

cp命令怎么在linux中使用

發(fā)布時間:2021-03-08 09:40:03 來源:億速云 閱讀:266 作者:Leah 欄目:系統(tǒng)運維

cp命令怎么在linux中使用?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

什么是Linux系統(tǒng)

Linux是一種免費使用和自由傳播的類UNIX操作系統(tǒng),是一個基于POSIX的多用戶、多任務(wù)、支持多線程和多CPU的操作系統(tǒng),使用Linux能運行主要的Unix工具軟件、應(yīng)用程序和網(wǎng)絡(luò)協(xié)議。

1.命令格式:

用法:

    cp [選項]... [-T] 源 目的

       或:cp [選項]... 源... 目錄

       或:cp [選項]... -t 目錄 源...

2.命令功能:

將源文件復(fù)制至目標文件,或?qū)⒍鄠€源文件復(fù)制至目標目錄。

3.命令參數(shù):

-a, --archive    等于-dR --preserve=all
    --backup[=CONTROL    為每個已存在的目標文件創(chuàng)建備份
-b                類似--backup 但不接受參數(shù)
   --copy-contents        在遞歸處理是復(fù)制特殊文件內(nèi)容
-d                等于--no-dereference --preserve=links
-f, --force        如果目標文件無法打開則將其移除并重試(當 -n 選項
                    存在時則不需再選此項)
-i, --interactive        覆蓋前詢問(使前面的 -n 選項失效)
-H                跟隨源文件中的命令行符號鏈接
-l, --link            鏈接文件而不復(fù)制
-L, --dereference   總是跟隨符號鏈接
-n, --no-clobber   不要覆蓋已存在的文件(使前面的 -i 選項失效)
-P, --no-dereference   不跟隨源文件中的符號鏈接
-p                等于--preserve=模式,所有權(quán),時間戳
    --preserve[=屬性列表   保持指定的屬性(默認:模式,所有權(quán),時間戳),如果
               可能保持附加屬性:環(huán)境、鏈接、xattr 等
-R, -r, --recursive  復(fù)制目錄及目錄內(nèi)的所有項目

4.命令實例:

實例一:復(fù)制單個文件到目標目錄,文件在目標文件中不存在

命令:

cp log.log test5

輸出:

復(fù)制代碼

代碼如下:


[root@localhost test]# cp log.log test5
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-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ù)時,兩個文件的時間是不一樣的。在帶了-a參數(shù)時,兩個文件的時間是一致的。 

實例二:目標文件存在時,會詢問是否覆蓋

命令:

cp log.log test5

輸出:

復(fù)制代碼

代碼如下:


[root@localhost test]# cp log.log test5
cp:是否覆蓋“test5/log.log”? n
[root@localhost test]# cp -a log.log test5
cp:是否覆蓋“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標志,也還會詢問是否覆蓋。

實例三:復(fù)制整個目錄

命令:

輸出:

目標目錄存在時:

復(fù)制代碼

代碼如下:


[root@localhost test]# cp -a test3 test5 
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxr-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.log
drwxrwxrwx 2 root root 4096 10-28 14:47 test3

目標目錄不存在是:

復(fù)制代碼

代碼如下:


[root@localhost test]# cp -a test3 test4
[root@localhost test]# ll
-rw-r--r-- 1 root root 0 10-28 14:48 log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5
[root@localhost test]#

說明:

注意目標目錄存在與否結(jié)果是不一樣的。目標目錄存在時,整個源目錄被復(fù)制到目標目錄里面。

實例四:復(fù)制的 log.log 建立一個連結(jié)檔 log_link.log

命令:

cp -s log.log log_link.log

輸出:

復(fù)制代碼

代碼如下:


[root@localhost test]# cp -s log.log log_link.log
[root@localhost test]# ll
lrwxrwxrwx 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.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
drwxr-xr-x 3 root root 4096 10-28 15:11 test5

看完上述內(nèi)容,你們掌握cp命令怎么在linux中使用的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI