天天看點

每天進步一點點——linux——cp一            簡介二            指令格式三            常用參數四            不常用參數五            示例六            參考資料

一            簡介

cp指令用來複制檔案或者目錄,是Linux系統中最常用的指令之一。一般情況下,shell會設定一個别名,在指令行下複制檔案時,如果目标檔案已經存在,就會詢問是否覆寫,不管你是否使用-i參數。但是如果是在shell腳本中執行cp時,沒有-i參數時不會詢問是否覆寫。

[[email protected] ~]# alias

alias cp='cp -i'

二            指令格式

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

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

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

三            常用參數

-a, --archive                 等于-dR--preserve=all

           --backup[=CONTROL         為每個已存在的目标檔案建立備份

  -i,--interactive             覆寫前詢問(使前面的 -n 選項失效)

  -l,--link                    連結檔案而不複制

  -L,--dereference             總是跟随符号連結

  -n,--no-clobber              不要覆寫已存在的檔案(使前面的 -i 選項失效)

  -R,-r, --recursive           遞歸複制目錄及其子目錄内的所有内容

四            不常用參數

-a, --archive                 等于-dR --preserve=all

     --backup[=CONTROL         為每個已存在的目标檔案建立備份

 -b                            類似--backup 但不接受參數

     --copy-contents           在遞歸處理是複制特殊檔案内容

 -d                            等于--no-dereference--preserve=links

  -f,--force                   如果目标檔案無法打開則将其移除并重試(當 -n 選項

                                        存在時則不需再選此項)

  -i,--interactive             覆寫前詢問(使前面的 -n 選項失效)

 -H                            跟随源檔案中的指令行符号連結

  -l,--link                    連結檔案而不複制

  -L,--dereference             總是跟随符号連結

  -n,--no-clobber              不要覆寫已存在的檔案(使前面的 -i 選項失效)

  -P,--no-dereference          不跟随源檔案中的符号連結

 -p                            等于--preserve=模式,所有權,時間戳

     --preserve[=屬性清單      保持指定的屬性(預設:模式,所有權,時間戳),如果

                                        可能保持附加屬性:環境、連結、xattr 等

 -c                           sameas --preserve=context

     --sno-preserve=屬性清單   不保留指定的檔案屬性

     --parents                 複制前在目标目錄建立來源檔案路徑中的所有目錄

  -R,-r, --recursive           遞歸複制目錄及其子目錄内的所有内容

     --reflink[=WHEN]          控制克隆/CoW 副本。請檢視下面的内如。

     --remove-destination      嘗試打開目标檔案前先删除已存在的目的地

                                        檔案 (相對于 --force 選項)

     --sparse=WHEN             控制建立稀疏檔案的方式

     --strip-trailing-slashes  删除參數中所有源檔案/目錄末端的斜杠

  -s,--symbolic-link           隻建立符号連結而不複制檔案

  -S,--suffix=字尾             自行指定備份檔案的字尾

 -t,  --target-directory=目錄  将所有參數指定的源檔案/目錄

                                           複制至目标目錄

  -T,--no-target-directory     将目标目錄視作普通檔案

  -u,--update                 copy only whenthe SOURCE file is newer

                                 than thedestination file or when the

                                 destination file is missing

  -v,--verbose                explain what isbeing done

  -x,--one-file-system        stay on thisfile system

  -Z,--context=CONTEXT        set securitycontext of copy to CONTEXT

     --help            顯示此幫助資訊并退出

     --version         顯示版本資訊并退出

五            示例

5.1      複制單個檔案到目錄,檔案目标不存在

[[email protected]]$ tree

.

├── t2

├── t3

│?? └── t2

└── t4

[[email protected] t1]$ cp t4 ./t3/

[[email protected]]$ tree

.

├── t2

├── t3

│?? ├── t2

│?? └── t4

└── t4

5.1      複制單個檔案到目錄,檔案目标存在,加-a與不加-a差別

[[email protected]]# ll

總用量 8

drwxrwxr-x 2 bzfys bzfys 4096 6月  18 09:16 t2

drwxrwxr-x 3 bzfys bzfys 4096 6月  18 09:18 t3

-rw-rw-r-- 1 bzfys bzfys    0 6月  18 09:17 t4

[[email protected]]# cp t4 t3/

cp:是否覆寫"t3/t4"? y

[[email protected]]# ll t3/

總用量 4

drwxrwxr-x 2 bzfys bzfys 4096 6月  18 09:16 t2

-rw-rw-r-- 1 bzfys bzfys    0 6月  18 09:22 t4

[[email protected]]# cp -p t4 t3/

cp:是否覆寫"t3/t4"? y

[[email protected]]# ll t3/

總用量 4

drwxrwxr-x 2 bzfys bzfys 4096 6月  18 09:16 t2

-rw-rw-r-- 1 bzfys bzfys    0 6月  18 09:17 t4

不難看出,如果有-p參數拷貝的檔案時間戳等屬性也不會變

5.2  複制目錄

[[email protected]]# cp t3 t5

cp: 略過目錄"t3"

[[email protected]]# cp -a t3 t5

[[email protected]]# tree

.

├── t2

├── t3

│?? ├── t2

│?? └── t4

├── t4

└── t5

    ├── t2

    └── t4

5 directories, 3 files

可以看出如果要複制目錄需要-a或者-r參數(-r參數後面示範)

5.3      複制一個目錄到另一個目錄

[[email protected]]# cp t3 t6/

cp: 略過目錄"t3

[[email protected]]# cp -r t3 t6

[[email protected]]# tree

.

├── t2

├── t3

│?? ├── t2

│?? └── t4

├── t4

├── t5

│?? ├── t2

│?? └── t4

└── t6

    └── t3

       ├── t2

       └── t4

8 directories, 4 files

[[email protected]]# cp -r t3/ t6/

cp:是否覆寫"t6/t3/t4"? y

[[email protected]]# tree

.

├── t2

├── t3

│?? ├── t2

│?? └── t4

├── t4

├── t5

│?? ├── t2

│?? └── t4

└── t6

    └── t3

       ├── t2

       └── t

我們可以看出,加不加/最後的結果是一樣的

六            參考資料

http://vbird.dic.ksu.edu.tw/

http://gnu.org/licenses/gpl.html

man手冊



繼續閱讀