Linux學習筆記11 cp命令

2022-01-30 09:46:51 字數 2893 閱讀 5182

功能說明:複製檔案或目錄。

語  法:cp [-abdfilpprrsuvx][-s 《備份字尾字串》][-v 《備份方式》][--help][--spares=《使用時機》][--version][原始檔或目錄][目標檔案或目錄] [目的目錄]

補充說明:cp指令用在複製檔案或目錄,如同時指定兩個以上的檔案或目錄,且最後的目的地是乙個已經存在的目錄,則它會把前面指定的所有檔案或目錄複製到該目錄中。若同時指定多個檔案或目錄,而最後的目的地並非是乙個已存在的目錄,則會出現錯誤資訊。

參  數:

-a或--archive  等同於"-dpr"引數,盡可能將檔案狀態、許可權等資料都照原狀予以複製。

-b或--backup  刪除,覆蓋目標檔案之前的備份,備份檔案會在字尾加上乙個備份字串。

-d或--no-dereference  當複製符號連線時,把目標檔案或目錄也建立為符號連線,並指向與原始檔或目錄連線的原始檔案或目錄。

-f或--force  強行複製檔案或目錄,不論目標檔案或目錄是否已存在。

-i或--interactive  覆蓋既有檔案之前先詢問使用者。

-l或--link  對原始檔建立硬連線,而非複製檔案。

-p或--preserve  保留原始檔或目錄的屬性。

-p或--parents  保留原始檔或目錄的路徑。

-r  遞迴處理,將指定目錄下的檔案與子目錄一併處理。

-r或--recursive  遞迴處理,將指定目錄下的所有檔案與子目錄一併處理。

-s或--symbolic-link  對原始檔建立符號連線,而非複製檔案。

-s《備份字尾字串》或--suffix=《備份字尾字串》  用"-b"引數備份目標檔案後,備份檔案的字尾會被加上乙個備份字串,預設的備份字尾字串是符號"~"。

-u或--update  使用這項引數後只會在原始檔的更改時間較目標檔案更新時或是 名稱相互對應的目標檔案並不存在,才複製檔案。

-v或--verbose  顯示指令執行過程。

-v《備份方式》或--version-control=《備份方式》  用"-b"引數備份目標檔案後,備份檔案的字尾會被加上乙個備份字串,這字串不僅可用"-s"引數變更,當使用"-v"引數指定不同備份方式時,也會產生不同字尾的備份字串。 

-x或--one-file-system  複製的檔案或目錄存放的檔案系統,必須與cp指令執行時所處的檔案系統相同,否則不予複製。

上機操作:

[ok_008@centos4 ok008filed]$ ls

mytxt.txt  untar

[ok_008@centos4 ok008filed]$ cp mytxt.txt mytxt1.txt --將檔案mytxt.txt複製,並命名為mytxt1.txt

[ok_008@centos4 ok008filed]$ ls

mytxt1.txt  mytxt.txt  untar

--複製檔案到乙個某一目錄下。

[ok_008@centos4 ok008filed]$ mkdir mydir --先建立乙個新的目錄mydir

[ok_008@centos4 ok008filed]$ ls

mydir  mytxt1.txt  mytxt.txt  untar

[ok_008@centos4 ok008filed]$ cd mydir

[ok_008@centos4 mydir]$ cd ..

[ok_008@centos4 ok008filed]$ cp *.txt mydir --複製字尾名為txt的所有檔案到mydir目錄下

[ok_008@centos4 ok008filed]$ cd mydir

[ok_008@centos4 mydir]$ ls

mytxt1.txt  mytxt.txt

[ok_008@centos4 mydir]$

[ok_008@centos4 mydir]$ rm *.txt 

--刪除mydir目錄下的所有.txt檔案

[ok_008@centos4 mydir]$ ls

[ok_008@centos4 mydir]$ cp ~/ok008filed/*.txt . --複製當前使用者目錄ok008filed下的所有.txt檔案到當前mydir目錄下,這裡注意不要忘記後面的".","."在linux中表示當前目錄

[ok_008@centos4 mydir]$ ls

mytxt1.txt  mytxt.txt

[ok_008@centos4 mydir]$

--複製ok008filed目錄下的檔案到使用者目錄下。

[ok_008@centos4 ok008filed]$ ls

mydir  mytxt1.txt  mytxt.txt  untar

[ok_008@centos4 ok008filed]$ cd ~/ --返回使用者目錄

[ok_008@centos4 ~]$ ls

ok008filed  testfile   --使用者目錄下只有ok008filed和testfile

[ok_008@centos4 ~]$ cd ok008filed

[ok_008@centos4 ok008filed]$ cp *.txt untar ~/  --複製*.txt和untar檔案到使用者目錄下。

[ok_008@centos4 ok008filed]$ cd ..

[ok_008@centos4 ~]$ ls

mytxt1.txt  mytxt.txt  ok008filed  testfile  untar  --說明複製成功。

[ok_008@centos4 ~]$

--參考資料《linux系統開發員》《linux常用命令全集》

今天學習內容不是很多,重要的是堅持和發現問題並解決問題,然後總結問題,剩餘點時間留學習點

oracle資料庫。

Linux學習筆記 cp複製命令

語法 和mv類似 cp option t source dest cp option source directory cp option t directory source description copy source to dest,or multiple source s to direc...

Linux命令 cp命令

cp命令用來複製檔案或者目錄,是linux系統中最常用的命令之一。1,語法 cp options source dest 或者cp options source.directory 2,引數說明 f 覆蓋已經存在的目標檔案而不給出提示。i 與 f選項相反,在覆蓋目標檔案之前給出提示,要求使用者確認是...

linux之cp命令grep命令學習

cp命令就是在linux下對檔案複製的乙個命令,功能很強大啊。其命令格式如下 cp 複製檔案或目錄 cp adfilprsu source destination cp options source1 source2 source3 directory 引數 a 相當於 pdr 的意思 d 若 檔案...