cp命令用來將一個或多個源文件或者目錄復制到指定的目的文件或目錄。它可以將單個源文件復制成一個指定文件名的具體的文件或一個已經存在的目錄下。cp命令還支持同時復制多個文件,當一次復制多個文件時,目標文件參數必須是一個已經存在的目錄,否則將出現錯誤。[b] (1)用法:[/b]
用法: cp [選項]... [-T] 源文件 目標文件 或: cp [選項]... 源文件... 目錄
或: cp [選項]... -t 目錄 源文件...[b] (2)功能:[/b]
將源文件復制至目標文件,或將多個源文件復制至目標目錄。[b] (3)選項參數:[/b]
1) -f: 強行復制文件或目錄,不論目標文件或目錄是否已存在 2) -i: 覆蓋既有文件之前先詢問用戶
3) -s: 對源文件建立符號連接,而非復制文件 4) -b: 覆蓋已存在的文件目標前將目標文件備份
5) -v: 詳細顯示命令執行的操作。 6) -f: 強行復制文件或目錄,不論目標文件或目錄是否已存在
7) -i: 覆蓋既有文件之前先詢問用戶;
8) -a: 等於-dR --preserve=all 9) -R, -r, --recursive 遞歸復制目錄及其子目錄內的所有內容
10) -d 等於--no-dereference --preserve=links [b] (4)實例:[/b]
1)[root@localhost Document]# cp mytext cpDir 將文檔拷貝到另一個文件夾下,該文件夾下沒有同名文檔
[root@localhost Document]# mkdir cpDir [root@localhost Document]# ll 總用量 0 drwxr-xr-x. 2 root root 6 5月 6 21:20 cpDir [root@localhost Document]# cat >mytext <<EOF > this is mytext! > EOF [root@localhost Document]# ll 總用量 4 drwxr-xr-x. 2 root root 6 5月 6 21:20 cpDir -rw-r--r--. 1 root root 16 5月 6 21:21 mytext [root@localhost Document]# cp mytext cpDir [root@localhost Document]# ls -l ./cpDir 總用量 4 -rw-r--r--. 1 root root 16 5月 6 21:22 mytext [root@localhost Document]# cat mytext this is mytext! [root@localhost Document]# ll 總用量 4 drwxr-xr-x. 2 root root 19 5月 6 21:22 cpDir -rw-r--r--. 1 root root 16 5月 6 21:21 mytext
2)[root@localhost Document]# cp mytext cpDir 將文檔拷貝到另一個文件夾下,該文件夾下有同名文檔,因此會提醒是否覆蓋。
[root@localhost Document]# ll 總用量 4 drwxr-xr-x. 2 root root 19 5月 6 21:22 cpDir -rw-r--r--. 1 root root 16 5月 6 21:21 mytext [root@localhost Document]# cat >mytext <<EOF > Modify the text ! > EOF [root@localhost Document]# cp mytext cpDir cp:是否覆蓋"cpDir/mytext"? y [root@localhost Document]# cat ./cpDir/mytext Modify the text !
3)[root@localhost Document]# cp newDir Dir1 將newDir拷貝一份到Dir1目錄下(當Dir1文件夾存在時);將newDir下的所有東西拷貝一份到新建的Dir2目錄(Dir2不存在)
[root@localhost Document]# ll
總用量 0
[root@localhost Document]# mkdir newDir
[root@localhost Document]# touch ./newDir/{text1.txt,text2.txt}
[root@localhost Document]# ll
總用量 0
drwxr-xr-x. 2 root root 38 5月 6 21:36 newDir
[root@localhost Document]# mkdir Dir1
[root@localhost Document]# cp newDir Dir1 //沒加-a參數會報錯
cp: 略過目錄"newDir"
[root@localhost Document]# cp -a newDir Dir1 //Dir1存在
[root@localhost Document]# cp -a newDir Dir2 //Dir2不存在
[root@localhost Document]# ll
總用量 0
drwxr-xr-x. 3 root root 19 5月 6 21:37 Dir1
drwxr-xr-x. 2 root root 38 5月 6 21:36 Dir2
drwxr-xr-x. 2 root root 38 5月 6 21:36 newDir
[root@localhost Document]# ls ./Dir1 //存在的效果
newDir
[root@localhost Document]# ls ./Dir2 //不存在的效果
text1.txt text2.txt4)[root@localhost newDir]# cp -s text1.txt t1_link 建立一個指向text1.txt的快捷方式 :t1_link
[root@localhost Document]# cp -s newDir newDir_link //貌似文件夾的快捷方式只用-s參數不能創建 cp: 略過目錄"newDir" [root@localhost Document]# ll 總用量 0 drwxr-xr-x. 3 root root 19 5月 6 21:37 Dir1 drwxr-xr-x. 2 root root 38 5月 6 21:36 Dir2 drwxr-xr-x. 2 root root 38 5月 6 21:36 newDir [root@localhost Document]# cd newDir [root@localhost newDir]# ll 總用量 0 -rw-r--r--. 1 root root 0 5月 6 21:36 text1.txt -rw-r--r--. 1 root root 0 5月 6 21:36 text2.txt [root@localhost newDir]# cp -s text1.txt t1_link [root@localhost newDir]# ll 總用量 0 lrwxrwxrwx. 1 root root 9 5月 6 21:43 t1_link -> text1.txt -rw-r--r--. 1 root root 0 5月 6 21:36 text1.txt -rw-r--r--. 1 root root 0 5月 6 21:36 text2.txt
5)[root@localhost Document]# cp -as newDir newDir_link 創建文件夾的快捷方式
[root@localhost Document]# cp -as newDir newDir_link //a參數指定所有的newDir包括其內的內容創建快捷方式,但這裡貌似有點限制 cp: "newDir_link/text1.txt":只能於當前目錄中創建相對的符號鏈接 cp: "newDir_link/text2.txt":只能於當前目錄中創建相對的符號鏈接 cp: "newDir_link/t1_link":只能於當前目錄中創建相對的符號鏈接 [root@localhost Document]# ll 總用量 0 drwxr-xr-x. 3 root root 19 5月 6 21:37 Dir1 drwxr-xr-x. 2 root root 38 5月 6 21:36 Dir2 drwxr-xr-x. 2 root root 52 5月 6 21:43 newDir drwxr-xr-x. 2 root root 6 5月 6 21:43 newDir_link //文件夾快捷方式創建成功
6)[root@localhost Document]# cp -a newDir text1與[root@localhost Document]# cp -r newDir text2
[root@localhost Document]# ll
總用量 0
[root@localhost Document]# mkdir newDir
[root@localhost Document]# touch ./newDir/{t1,t2}
[root@localhost Document]# mkdir text1 text2
[root@localhost Document]# cp -a newDir text1 //text1用-a參數
[root@localhost Document]# cp -r newDir text2 //text2用-r參數
[root@localhost Document]# ls -l text1/*
總用量 0
-rw-r--r--. 1 root root 0 5月 6 22:02 t1
-rw-r--r--. 1 root root 0 5月 6 22:02 t2
[root@localhost Document]# ls -l text2/*
總用量 0
-rw-r--r--. 1 root root 0 5月 6 22:03 t1
-rw-r--r--. 1 root root 0 5月 6 22:03 t2
[root@localhost Document]# cp -a newDir text3
[root@localhost Document]# cp -r newDir text4
[root@localhost Document]# ls -l text3/*
-rw-r--r--. 1 root root 0 5月 6 22:02 text3/t1
-rw-r--r--. 1 root root 0 5月 6 22:02 text3/t2
[root@localhost Document]# ls -l text4/*
-rw-r--r--. 1 root root 0 5月 6 22:04 text4/t1
-rw-r--r--. 1 root root 0 5月 6 22:04 text4/t2
[root@localhost Document]# ll
總用量 0
drwxr-xr-x. 2 root root 30 5月 6 22:02 newDir
drwxr-xr-x. 3 root root 19 5月 6 22:03 text1
drwxr-xr-x. 3 root root 19 5月 6 22:03 text2
drwxr-xr-x. 2 root root 30 5月 6 22:02 text3
drwxr-xr-x. 2 root root 30 5月 6 22:04 text4
[root@localhost Document]# cd text1
[root@localhost text1]# ll
總用量 0
drwxr-xr-x. 2 root root 30 5月 6 22:02 newDir
[root@localhost text1]# cd ../text2
[root@localhost text2]# ll
總用量 0
drwxr-xr-x. 2 root root 30 5月 6 22:03 newDir
[root@localhost text2]# cd ../text3
[root@localhost text3]# ll
總用量 0
-rw-r--r--. 1 root root 0 5月 6 22:02 t1
-rw-r--r--. 1 root root 0 5月 6 22:02 t2
[root@localhost text3]# cd ../text4
[root@localhost text4]# ll
總用量 0
-rw-r--r--. 1 root root 0 5月 6 22:04 t1
-rw-r--r--. 1 root root 0 5月 6 22:04 t2 //-a和-r沒有差別,cp命令的差別主要集中在第二個參數是否存在上,與此同時,ls -l命令的效果也有所差別7)[root@localhost Document]# cp mytext{,.txt} 復制一份加上後綴存起來
[root@localhost Document]# touch mytext
[root@localhost Document]# cp mytext{,.txt}
[root@localhost Document]# ll
總用量 0
-rw-r--r--. 1 root root 0 5月 6 22:15 mytext
-rw-r--r--. 1 root root 0 5月 6 22:15 mytext.txt
drwxr-xr-x. 2 root root 30 5月 6 22:02 newDir
drwxr-xr-x. 3 root root 19 5月 6 22:03 text1
drwxr-xr-x. 3 root root 19 5月 6 22:03 text2
drwxr-xr-x. 2 root root 30 5月 6 22:02 text3
drwxr-xr-x. 2 root root 30 5月 6 22:04 text48)[root@localhost Document]# cp --help
[root@localhost Document]# cp --help
用法:cp [選項]... [-T] 源文件 目標文件
或:cp [選項]... 源文件... 目錄
或:cp [選項]... -t 目錄 源文件...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.
Mandatory arguments to long options are mandatory for short options too.
-a, --archive 等於-dR --preserve=all
--attributes-only 僅復制屬性而不復制數據 --backup[=CONTROL 為每個已存在的目標文件創建備份
-b 類似--backup 但不接受參數
--copy-contents 在遞歸處理是復制特殊文件內容
-d 等於--no-dereference --preserve=links
-f, --force if an existing destination file cannot be
opened, remove it and try again (this option
is ignored when the -n option is also used)
-i, --interactive prompt before overwrite (overrides a previous -n
option)
-H follow command-line symbolic links in SOURCE
-l, --link hard link files instead of copying
-L, --dereference always follow symbolic links in SOURCE
-n, --no-clobber 不要覆蓋已存在的文件(使前面的 -i 選項失效)
-P, --no-dereference 不跟隨源文件中的符號鏈接
-p 等於--preserve=模式,所有權,時間戳
--preserve[=屬性列表 保持指定的屬性(默認:模式,所有權,時間戳),如果
可能保持附加屬性:環境、鏈接、xattr 等
-c deprecated, same as --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 只在源文件比目標文件新,或目標文件
不存在時才進行復制
-v, --verbose 顯示詳細的進行步驟
-x, --one-file-system 不跨越文件系統進行操作
-Z, --context[=CTX] set SELinux security context of destination
file to default type, or to CTX if specified
--help 顯示此幫助信息並退出
--version 顯示版本信息並退出
默認情況下,源文件的稀疏性僅僅通過簡單的方法判斷,對應的目標文件目標文件也
被為稀疏。這是因為默認情況下使用了--sparse=auto 參數。如果明確使用
--sparse=always 參數則不論源文件是否包含足夠長的0 序列也將目標文件創文
建為稀疏件。
使用--sparse=never 參數禁止創建稀疏文件。
當指定了--reflink[=always] 參數時執行輕量化的復制,即只在數據塊被修改的
情況下才復制。如果復制失敗或者同時指定了--reflink=auto,則返回標准復制模式。
The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.
The version control method may be selected via the --backup option or through
the VERSION_CONTROL environment variable. Here are the values:
none, off 不進行備份(即使使用了--backup 選項)
numbered, t 備份文件加上數字進行排序
existing, nil 若有數字的備份文件已經存在則使用數字,否則使用普通方式備份
simple, never 永遠使用普通方式備份
有一個特別情況:如果同時指定--force 和--backup 選項,而源文件和目標文件
是同一個已存在的一般文件的話,cp 會將源文件備份。
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
請向<http://translationproject.org/team/zh_CN.html> 報告cp 的翻譯錯誤
要獲取完整文檔,請運行:info coreutils 'cp invocation'9)[root@localhost Document]# cp --version
[root@localhost Document]# cp --version cp (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. 許可證:GPLv3+:GNU 通用公共許可證第3 版或更新版本<http://gnu.org/licenses/gpl.html>。 本軟件是自由軟件:您可以自由修改和重新發布它。 在法律范圍內沒有其他保證。 由Torbjörn Granlund、David MacKenzie 和Jim Meyering 編寫。
[b] (5)其他;[/b] 注意:
一般情況下,shell會設置一個別名,在命令行下復制文件時,如果目標文件已經存在,就會詢問是否覆蓋,不管你是否使用-i參數。但是如果是在shell腳本中執行cp時,沒有-i參數時不會詢問是否覆蓋。這說明命令行和shell腳本的執行方式有些不同。命令行與shell腳本:
shell命令就是你說的終端的命令,shell翻譯成殼的意思,它是包裹在linux內核外層的,一個可通過一系列的linux命令對操作系統發出相關指令的人機界面。shell可以通過其條件語句和循環語句等,把一系列linux命令結合在一起,形成一個相當於面向過程的程序,shell script,來實現一些較為復雜的功能。 總括,shell是linux命令集的概稱,是屬於命令行的人機界面。linux的shell script 是由命令加一些條件組合起來的。
shell命令就是終端命令,shell編程其實和windows的批處理差不多,區別的是,shell的語言功能比批處理強大