命令用途
cp命令用于復(fù)制文件或目錄
1. 如同時指定兩個以上的文件或目錄,且最后的目的地是一個已經(jīng)存在的目錄,則它會把前面指定的所有文件或目錄復(fù)制到此目錄中。
2. 若同時指定多個文件或目錄,而最后的目的地并非一個已存在的目錄,則會出現(xiàn)錯誤信息命令舉例
#幫助命令
bixiaopeng@bixiaopeng-To-be-filled-by-O-E-M:~$ cp --h
用法:cp [選項]... [-T] 源文件 目標(biāo)文件
或:cp [選項]... 源文件... 目錄
或:cp [選項]... -t 目錄 源文件...
將源文件復(fù)制至目標(biāo)文件,或?qū)⒍鄠€源文件復(fù)制至目標(biāo)目錄。
長選項必須使用的參數(shù)對于短選項時也是必需使用的。
-a, --archive 等于-dR --preserve=all
--attributes-only 僅復(fù)制屬性而不復(fù)制數(shù)據(jù) --backup[=CONTROL 為每個已存在的目標(biāo)文件創(chuàng)建備份
-b 類似--backup 但不接受參數(shù)
--copy-contents 在遞歸處理是復(fù)制特殊文件內(nèi)容
-d 等于--no-dereference --preserve=links
-f, --force 如果目標(biāo)文件無法打開則將其移除并重試(當(dāng) -n 選項
存在時則不需再選此項)
-i, --interactive 覆蓋前詢問(使前面的 -n 選項失效)
-H 跟隨源文件中的命令行符號鏈接
-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=模式,所有權(quán),時間戳
--preserve[=屬性列表 保持指定的屬性(默認:模式,所有權(quán),時間戳),如果
可能保持附加屬性:環(huán)境、鏈接、xattr 等
--sno-preserve=屬性列表 不保留指定的文件屬性
--parents 復(fù)制前在目標(biāo)目錄創(chuàng)建來源文件路徑中的所有目錄
-R, -r, --recursive 遞歸復(fù)制目錄及其子目錄內(nèi)的所有內(nèi)容
--reflink[=WHEN] 控制克隆/CoW 副本。請查看下面的內(nèi)如。
--remove-destination 嘗試打開目標(biāo)文件前先刪除已存在的目的地
文件 (相對于 --force 選項)
--sparse=WHEN 控制創(chuàng)建稀疏文件的方式
--strip-trailing-slashes 刪除參數(shù)中所有源文件/目錄末端的斜杠
-s, --symbolic-link 只創(chuàng)建符號鏈接而不復(fù)制文件
-S, --suffix=后綴 自行指定備份文件的后綴
-t, --target-directory=目錄 將所有參數(shù)指定的源文件/目錄
復(fù)制至目標(biāo)目錄
-T, --no-target-directory 將目標(biāo)目錄視作普通文件
-u, --update 只在源文件比目標(biāo)文件新,或目標(biāo)文件
不存在時才進行復(fù)制
-v, --verbose 顯示詳細的進行步驟
-x, --one-file-system 不跨越文件系統(tǒng)進行操作
--help 顯示此幫助信息并退出
--version 顯示版本信息并退出
默認情況下,源文件的稀疏性僅僅通過簡單的方法判斷,對應(yīng)的目標(biāo)文件目標(biāo)文件也
被為稀疏。這是因為默認情況下使用了--sparse=auto 參數(shù)。如果明確使用
--sparse=always 參數(shù)則不論源文件是否包含足夠長的0 序列也將目標(biāo)文件創(chuàng)文
建為稀疏件。
使用--sparse=never 參數(shù)禁止創(chuàng)建稀疏文件。
當(dāng)指定了--reflink[=always] 參數(shù)時執(zhí)行輕量化的復(fù)制,即只在數(shù)據(jù)塊被修改的
情況下才復(fù)制。如果復(fù)制失敗或者同時指定了--reflink=auto,則返回標(biāo)準(zhǔn)復(fù)制模式。
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 備份文件加上數(shù)字進行排序
existing, nil 若有數(shù)字的備份文件已經(jīng)存在則使用數(shù)字,否則使用普通方式備份
simple, never 永遠使用普通方式備份
有一個特別情況:如果同時指定--force 和--backup 選項,而源文件和目標(biāo)文件
是同一個已存在的一般文件的話,cp 會將源文件備份。
#環(huán)境準(zhǔn)備
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ mkdir dir1
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ mkdir dir2
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ touch dir1/f1.txt
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ touch dir2/f2.txt
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cd dir1
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ ls -al
total 0
drwxr-xr-x 3 bixiaopeng staff 102 9 18 16:15 .
drwxr-xr-x 4 bixiaopeng staff 136 9 18 16:14 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:15 f1.txt
#拷貝兩個文件
# 拷貝f1.txt為cpf1.txt
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp f1.txt cpf1.txt
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ ls -al
total 0
drwxr-xr-x 4 bixiaopeng staff 136 9 18 16:23 .
drwxr-xr-x 4 bixiaopeng staff 136 9 18 16:14 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:23 cpf1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:15 f1.txt
#采用交互方式拷貝文件
#采用交互方式將文件f1.txt拷貝為cpf12.txt
#1. 如果cpf12.txt不存在,則直接復(fù)制成功
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -i f1.txt cpf12.txt
#2. 如果cpf12.txt已存在,則會出現(xiàn)提示是否覆蓋原來的,y就會覆蓋
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -i f1.txt cpf12.txt
overwrite cpf12.txt? (y/n [n]) y
#3. 如果cpf12.txt已存在,則會出現(xiàn)提示是否覆蓋原來的,n就不會覆蓋
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -i f1.txt cpf12.txt
overwrite cpf12.txt? (y/n [n]) n
not overwritten
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ ls -al
total 0
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:40 .
drwxr-xr-x 4 bixiaopeng staff 136 9 18 16:14 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:23 cpf1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:40 cpf12.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:15 f1.txt
#強制覆蓋
#如果cpf12.txt已經(jīng)存在,也可以使用強制覆蓋
bixiaopeng@bixiaopengtekiMacBook-Pro dir1$ cp -f f1.txt cpf12.txt
#拷貝目錄
#將目錄dir1復(fù)制成dir11
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -R dir1 dir11
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir11
total 0
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:50 .
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:50 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:50 cpf1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:50 cpf12.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:50 f1.txt
#同時拷貝文件和目錄
#新建一個目錄dir3
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ mkdir dir3
#將某些文件和目錄復(fù)制到dir3目錄下
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -R dir11/cpf1.txt dir11/cpf12.txt dir1 dir3
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir3
total 0
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:53 .
drwxr-xr-x 6 bixiaopeng staff 204 9 18 16:52 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:53 cpf1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:53 cpf12.txt
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:53 dir1
#拷貝的文件保留屬性
# -p 或 --preserve 保留源文件或目錄的屬性,包括所有者、所屬組、權(quán)限與時間
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -p dir2/f2.txt f2.txt
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al
total 0
drwxr-xr-x 7 bixiaopeng staff 238 9 18 16:58 .
drwxr-xr-x@ 7 bixiaopeng staff 238 9 18 15:43 ..
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:40 dir1
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:50 dir11
drwxr-xr-x 3 bixiaopeng staff 102 9 18 16:15 dir2
drwxr-xr-x 5 bixiaopeng staff 170 9 18 16:53 dir3
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:15 f2.txt
# -P 或 --parents 保留源文件或目錄的路徑,此路徑可以是絕對路徑或相對路徑,且目的目錄必須已經(jīng)存在
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -P dir2/f2.txt f3.txt
# -b備份,MAC上不支持該命令,linux系統(tǒng)上可以用
#目錄合并及文件覆蓋
#查看目錄dir1
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir1
total 0
drwxr-xr-x 7 bixiaopeng staff 238 9 18 17:27 .
drwxr-xr-x 8 bixiaopeng staff 272 9 18 17:00 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:23 cpf1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:48 cpf12.txt
drwxr-xr-x 2 bixiaopeng staff 68 9 18 17:27 ddddir
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:15 f1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 17:27 f2.txt
#查看目錄dir2
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir2
total 0
drwxr-xr-x 4 bixiaopeng staff 136 9 18 17:29 .
drwxr-xr-x 8 bixiaopeng staff 272 9 18 17:00 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 17:28 f1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 17:28 f2.txt
# 合并兩個目錄
# -f 強制覆蓋 -r 拷貝目錄和文件 -v 顯示過程 -p 保留原有屬性
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ cp -frvp dir1/* dir2
dir1/cpf1.txt -> dir2/cpf1.txt
dir1/cpf12.txt -> dir2/cpf12.txt
dir1/ddddir -> dir2/ddddir
dir1/f1.txt -> dir2/f1.txt
dir1/f2.txt -> dir2/f2.txt
#再查看dir2全部都復(fù)制過來啦
bixiaopeng@bixiaopengtekiMacBook-Pro testshell$ ls -al dir2
total 0
drwxr-xr-x 7 bixiaopeng staff 238 9 18 17:31 .
drwxr-xr-x 8 bixiaopeng staff 272 9 18 17:00 ..
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:23 cpf1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:48 cpf12.txt
drwxr-xr-x 2 bixiaopeng staff 68 9 18 17:27 ddddir
-rw-r--r-- 1 bixiaopeng staff 0 9 18 16:15 f1.txt
-rw-r--r-- 1 bixiaopeng staff 0 9 18 17:27 f2.txt