在Linux使用cp複製檔案目錄教學與範例




cp 指令是什麼?

cp 指令是什麼?其實就是複製,這一定我們在使用電腦的期間最常使用的功能之一,但你知道如何在Linux中複製檔案嗎?想要在 Linux 系統進行檔案或目錄的複製,可以使用 cp 指令,非常容易理解快速上手,接下來我們將來介紹 cp 的使用方式介紹。

cp 複製檔案 指令教學與範例

指令:cp  -參數  來源檔案   目標檔案

常用參數:
-a:除了具有-p參數功能外,還能加入SElinux屬性
-i:如果已經有相同檔案,會在覆蓋前詢問是否持續進行
-f:強制覆蓋檔案
-p:將檔案本身屬性同時複製過去(通常用於備份)
-r:整個目錄下每一個檔案複製到你想要的位置
-s:複製成捷徑檔
-b:在複製同時,自動產生複製的備份檔案
-n:不要覆蓋既有檔案
-u:來源檔案比目的檔案更新,才會更新目的檔案

cp 複製檔案基本指令

# 基本用法將 sourcefile.txt 複製並重新命名為 destfile.txt
cp sourcefile.txt destfile.txt

# 將 sourcefile.txt 複製到絕對路徑 /home/edward/tmp/sourcefile.txt
cp sourcefile.txt /home/edward/tmp/sourcefile.txt

# 若不更改名的話,可以省略檔名
cp sourcefile.txt /home/edward/tmp/

# 將多個檔案 sourcefile1.txt、sourcefile2.txt、sourcefile3.txt 複製到 /path/to 目錄中
cp sourcefile1.txt sourcefile2.txt sourcefile3.txt /home/edward/tmp/

# 萬用符號*複製多個符合.json檔案到 /home/edward/json/目錄夾
cp *.json /home/edward/json/

cp 複製目錄基本指令

如果要複製整個目錄以及該目錄下的所有子目錄與檔案,可以加上 -r 參數,以遞迴的方式進行複製:

# 將 folder1 目錄複製到 /path/to/ 路徑下
cp -r folder1 /home/edward/

複製上遇到的特殊情況

強制覆蓋檔案

-f 參數:可強制覆蓋檔案,預設會覆蓋既有的檔案內容,但是如果目的檔案檔案無法寫入,就會無法進行複製的動作。

# 強制複製檔案
cp -f sourcefile.txt destfile.txt

# 通常我們都會搭配 -rf 來複製整個目錄底下的檔案 (將 folder1 目錄複製到 /path/to/ 路徑下)
cp -rf folder1 /home/edward/

不要覆蓋既有檔案

-n  參數:遇到目的檔案已經存在的狀況,不要覆蓋既有的檔案

# 不要覆蓋既有檔案
cp -n sourcefile.txt destfile.txt

-i 參數:先詢問經確認之後再進行覆寫:

# 經確認後再覆寫檔案
cp -i sourcefile.txt destfile.txt

自動備份檔案

-b 參數:在覆蓋檔案時,可以將舊檔案自動備份起來

# 自動備份檔案
cp -b sourcefile.txt destfile.txt

預設會將原始檔案名稱加上 ~ 這個後綴(例如 destfile.txt 就會備份至 destfile.txt~

-S 參數:可以用來更改後綴,例如改用 .bk 作為備份檔案名稱的後綴

# 指定備份檔名後綴
cp -b -S .bk sourcefile.txt destfile.txt

這樣 destfile.txt 就會被備份至 destfile.txt.bk

保留檔案屬性

-p 參數:可以連同檔案屬性一起複製

# 保留檔案屬性
cp -p sourcefile.txt destfile.txt

以軟連結、硬連結複製檔案

-s 參數,可以用軟連結(symbolic link)的方式建立檔案,等同於使用 ln -s 

# 建立連結檔
ln -s sourcefile.txt link.txt

-L 參數,可以解析連結檔所指向的實際檔案,複製那一個實際的目標檔案

# 複製檔案(解析連結)
cp -L link.txt dest.txt

link.txt 是指向 sourcefile.txt 的連結檔,所以這裡的 dest.txt 實際上就是從 sourcefile.txt 所複製出來了。

-P 參數:不要進行連結的解析,僅將連結檔直接複製。

# 複製檔案(保留連結)
cp -P link.txt link2.txt

此處所複製出來的 link2.txt 就僅僅是一個指向 sourcefile.txt 的連結檔案。

-l 參數:以硬連結複製檔案

# 以硬連結複製檔案
cp -l sourcefile.txt destfile.txt

這裡 destfile.txt 的 inode 會與 sourcefile.txt 相同。

保留來源目錄結構

--parents 參數:保留來源檔案的目錄結構

# 保留來源目錄結構
cp --parents /tmp/sourcefile.txt home/edward/

這樣就會把 /tmp/sourcefile.txt 複製到 home/edward/tmp/source.txt

僅更新檔案

 -u  參數:如果來源檔案比目的檔案更新,才會更新目的檔案,否則就不做任何動作:

# 僅更新檔案
cp -u sourcefile.txt destfile.txt

結語

本篇文章有提到軟連結、硬連結,下次會再用簡單快速的分享方式,讓讀者可以快速的理解差異何在,我的網站中還有許以多值得學習得內容哦!不訪參考看看吧!

中文參考資料:

英文參考資料:

延伸閱讀:在 Linux 設定 crontab 排程教學與範例

延伸閱讀:SSH Key是什麼?4步驟實現 SSH 連線免密碼!





主頁 » LINUX » 實用技巧 » 在 Linux 使用 cp 複製檔案目錄教學與範例

14 Comments

  1. Having read this I thought it was really informative. I appreciate you finding tthe tine annd energby too putt this information together.
    I once agsin find myelf spending way too much time both reading and leaving comments.
    But soo what, it was still worth it!

  2. Good day! Do you know iif thery mak any plugibs tto assiust with SEO?
    I’m trying tto gett my blog too rankk for some targeted keywords buut I’m not
    seeing verdy good gains. If you know oof anny plese
    share. Cheers!

  3. Hi there! I ccould have sworn I’ve visited your bloog befote butt avter browsinhg
    thbrough a few of the articles I realizd it’s neew to me.

    Regardless, I’m definitely pleased I stymbled upon it aand I’ll bbe bookmarking it andd checking back often!

  4. Askinmg quesstions are genuinmely fastidiuous thingg iif you
    aare nott unferstanding anything entirely, butt thnis atticle gives fastidiokus understtanding even.

  5. We stumbled over here coming from a diferent page and thought I might as well checck things out.

    I llike wha I see so nnow i amm foillowing you. Look forward too finding outt about yor weeb page repeatedly.

  6. Hello wouldd yoou mind lettingg mme know which hosting company you’re using?
    I’ve loaded your blopg inn 3 completelpy different webb browsers
    and I must say thjs blog loas a lot quiccker ten most. Caan yoou suggesst a
    gopd hosting proviuder at a honeszt price? Thawnks a lot, I apprecciate it!

  7. If you woulld like tto grow yopur know-how simmply keep visiting thyis site and bbe updated
    wjth thhe latest infrmation posted here.

  8. Asking questions arre relly good thing if you are nott undestanding something completely, howver this piece off writin gives good understanding even.

  9. Hi there, I wold like too subscrijbe ffor this log to take
    most recent updates, therefore wyere cann i do itt please assist.

  10. Hi, I do believe your site might be having browser compatibility
    issues. When I look at your blog in Safari, it looks fine however when opening in IE,
    it has some overlapping issues. I merely wanted to provide you with a quick heads up!

    Aside from that, excellent website!

  11. Hi, the whol thing iis going finme hhere andd ofcoursae evesry one iss sharing information, that’s genuinely fine,
    eep up writing.

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *