【Linux】一步一步学Linux——cp命令(31)
00. 目錄
文章目錄
- 00. 目錄
- 01. 命令概述
- 02. 命令格式
- 03. 常用選項(xiàng)
- 04. 參考示例
- 05. 附錄
01. 命令概述
cp命令可以理解為英文單詞copy的縮寫(xiě),其功能為復(fù)制文件或目錄。
cp命令可以將多個(gè)文件復(fù)制到一個(gè)具體的文件名或一個(gè)已經(jīng)存在的目錄下,也可以同時(shí)復(fù)制多個(gè)文件到一個(gè)指定的目錄中。
02. 命令格式
用法:cp [選項(xiàng)]... [-T] 源文件 目標(biāo)文件或:cp [選項(xiàng)]... 源文件... 目錄或:cp [選項(xiàng)]... -t 目錄 源文件... Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.03. 常用選項(xiàng)
長(zhǎng)選項(xiàng)必須使用的參數(shù)對(duì)于短選項(xiàng)時(shí)也是必需使用的。-a, --archive 等于-dR --preserve=all--attributes-only 只拷貝文件屬性,不拷貝文件內(nèi)容--backup[=CONTROL 為每個(gè)已存在的目標(biāo)文件創(chuàng)建備份-b 類(lèi)似--backup 但不接受參數(shù)--copy-contents 在遞歸處理是復(fù)制特殊文件內(nèi)容-d 等于--no-dereference --preserve=links-f, --force 如果目標(biāo)文件無(wú)法打開(kāi)則將其移除并重試(當(dāng) -n 選項(xiàng)存在時(shí)則不需再選此項(xiàng))-i, --interactive 覆蓋前詢(xún)問(wèn)(使前面的 -n 選項(xiàng)失效)-H 跟隨源文件中的命令行符號(hào)鏈接-l, --link 鏈接文件而不復(fù)制-L, --dereference 總是跟隨符號(hào)鏈接-n, --no-clobber 不要覆蓋已存在的文件(使前面的 -i 選項(xiàng)失效)-P, --no-dereference 不跟隨源文件中的符號(hào)鏈接-p 等于--preserve=模式,所有權(quán),時(shí)間戳--preserve[=屬性列表 保持指定的屬性(默認(rèn):模式,所有權(quán),時(shí)間戳),如果可能保持附加屬性:環(huán)境、鏈接、xattr 等-c same as --preserve=context--sno-preserve=屬性列表 不保留指定的文件屬性--parents 復(fù)制前在目標(biāo)目錄創(chuàng)建來(lái)源文件路徑中的所有目錄-R, -r, --recursive 遞歸復(fù)制目錄及其子目錄內(nèi)的所有內(nèi)容--reflink[=WHEN] 控制克隆/CoW 副本。請(qǐng)查看下面的內(nèi)如。--remove-destination 嘗試打開(kāi)目標(biāo)文件前先刪除已存在的目的地文件 (相對(duì)于 --force 選項(xiàng))--sparse=WHEN 控制創(chuàng)建稀疏文件的方式--strip-trailing-slashes 刪除參數(shù)中所有源文件/目錄末端的斜杠-s, --symbolic-link 只創(chuàng)建符號(hào)鏈接而不復(fù)制文件-S, --suffix=后綴 自行指定備份文件的后綴-t, --target-directory=目錄 將所有參數(shù)指定的源文件/目錄復(fù)制至目標(biāo)目錄-T, --no-target-directory 將目標(biāo)目錄視作普通文件-u, --update copy only when the SOURCE file is newerthan the destination file or when thedestination file is missing-v, --verbose explain what is being done-x, --one-file-system stay on this file system-Z, --context=CONTEXT set security context of copy to CONTEXT--help 顯示此幫助信息并退出--version 顯示版本信息并退出默認(rèn)情況下,源文件的稀疏性?xún)H僅通過(guò)簡(jiǎn)單的方法判斷,對(duì)應(yīng)的目標(biāo)文件目標(biāo)文件也 被為稀疏。這是因?yàn)槟J(rèn)情況下使用了--sparse=auto 參數(shù)。如果明確使用 --sparse=always 參數(shù)則不論源文件是否包含足夠長(zhǎng)的0 序列也將目標(biāo)文件創(chuàng)文 建為稀疏件。 使用--sparse=never 參數(shù)禁止創(chuàng)建稀疏文件。當(dāng)指定了--reflink[=always] 參數(shù)時(shí)執(zhí)行輕量化的復(fù)制,即只在數(shù)據(jù)塊被修改的 情況下才復(fù)制。如果復(fù)制失敗或者同時(shí)指定了--reflink=auto,則返回標(biāo)準(zhǔn)復(fù)制模式。備份文件的后綴為"~",除非以--suffix 選項(xiàng)或是SIMPLE_BACKUP_SUFFIX 環(huán)境變量指定。版本控制的方式可通過(guò)--backup 選項(xiàng)或VERSION_CONTROL 環(huán)境 變量來(lái)選擇。以下是可用的變量值:none, off 不進(jìn)行備份(即使使用了--backup 選項(xiàng))numbered, t 備份文件加上數(shù)字進(jìn)行排序existing, nil 若有數(shù)字的備份文件已經(jīng)存在則使用數(shù)字,否則使用普通方式備份simple, never 永遠(yuǎn)使用普通方式備份有一個(gè)特別情況:如果同時(shí)指定--force 和--backup 選項(xiàng),而源文件和目標(biāo)文件 是同一個(gè)已存在的一般文件的話(huà),cp 會(huì)將源文件備份。04. 參考示例
4.1 復(fù)制文件
[deng@localhost test]$ cp /etc/passwd passwd [deng@localhost test]$ ls a passwd [deng@localhost test]$4.2 復(fù)制一個(gè)文件到目錄中
[deng@localhost test]$ ls a passwd [deng@localhost test]$ cp passwd a/**注意:**和 mv 命令不同的是:cp 拷貝,而mv相當(dāng)于剪切。
4.3 拷貝多個(gè)文件到指定目錄中
[deng@localhost test]$ cp a b c d dir/ [deng@localhost test]$ tree dir dir ├── a ├── b ├── c └── d0 directories, 4 files [deng@localhost test]$4.4 遞歸復(fù)制目錄及其子目錄內(nèi)的所有內(nèi)容
[deng@localhost test]$ cp -r /etc/systemd ./ [deng@localhost test]$ ls a b c d dir systemd [deng@localhost test]$注意:要拷貝一個(gè)目錄,需要添加 -r 或者 -R 選項(xiàng)來(lái)遞歸實(shí)現(xiàn)。
4.5 創(chuàng)建文件的硬鏈接,而不是拷貝
[deng@localhost test]$ ls -i a 204100129 a [deng@localhost test]$ cp -l a aa [deng@localhost test]$ ls -i a aa 204100129 a 204100129 aa [deng@localhost test]$拷貝文件意味著必須使用一些存儲(chǔ)空間來(lái)儲(chǔ)存拷貝的文件。有時(shí)候出于某種原因,可能想要?jiǎng)?chuàng)建“快捷方式”或者鏈接到文件,而不是拷貝。要做到這一點(diǎn),可以使用 -l 選項(xiàng)。
4.6 只創(chuàng)建符號(hào)鏈接而不復(fù)制文件
[deng@localhost test]$ cp -s a a_link [deng@localhost test]$ ls -l 總用量 0 -rw-rw-r-- 2 deng deng 0 7月 13 21:42 a -rw-rw-r-- 2 deng deng 0 7月 13 21:42 aa lrwxrwxrwx 1 deng deng 1 7月 13 21:47 a_link -> a -rw-rw-r-- 1 deng deng 0 7月 13 21:42 b -rw-rw-r-- 1 deng deng 0 7月 13 21:42 c -rw-rw-r-- 1 deng deng 0 7月 13 21:42 d drwxrwxr-x 2 deng deng 42 7月 13 21:42 dir drwxr-xr-x 4 deng deng 151 7月 13 21:44 systemd [deng@localhost test]$**注意:**創(chuàng)建符號(hào)鏈接只能在當(dāng)前目錄下進(jìn)行。
4.7 不跟隨源文件中的符號(hào)鏈接
只拷貝符號(hào)鏈接文件,可以用 -P 選項(xiàng)來(lái)實(shí)現(xiàn)。當(dāng)對(duì)符號(hào)鏈接使用 cp 命令,它默認(rèn)會(huì)拷貝它自身。
[deng@localhost test]$ ls -l a_link lrwxrwxrwx 1 deng deng 1 7月 13 21:47 a_link -> a [deng@localhost test]$ cp -P a_link a_link2 [deng@localhost test]$ ls -l a_link* lrwxrwxrwx 1 deng deng 1 7月 13 21:47 a_link -> a lrwxrwxrwx 1 deng deng 1 7月 13 21:51 a_link2 -> a [deng@localhost test]$顯然,cp 命令照原樣拷貝 debug_link.log 自身,文件類(lèi)型仍然是一個(gè)符號(hào)鏈接。
4.8 總是跟隨符號(hào)鏈接
可以試一下 -L 選項(xiàng)。基本上,這個(gè)剛好與上面的 -P 選項(xiàng)相反。也是cp默認(rèn)的行為。
[deng@localhost test]$ ls -l a_link lrwxrwxrwx 1 deng deng 1 7月 13 21:47 a_link -> a [deng@localhost test]$ cp -L a_link a_link3 [deng@localhost test]$ ls -l a_link3 -rw-rw-r-- 1 deng deng 0 7月 13 21:53 a_link3 [deng@localhost test]$4.9 拷貝目錄,符號(hào)鏈接也會(huì)被拷貝
[deng@localhost test]$ ls -l dir/ 總用量 0 -rw-rw-r-- 1 deng deng 0 7月 13 21:42 a lrwxrwxrwx 1 deng deng 1 7月 13 21:47 a_link -> a lrwxrwxrwx 1 deng deng 1 7月 13 21:51 a_link2 -> a -rw-rw-r-- 1 deng deng 0 7月 13 21:53 a_link3 -rw-rw-r-- 1 deng deng 0 7月 13 21:42 b -rw-rw-r-- 1 deng deng 0 7月 13 21:42 c -rw-rw-r-- 1 deng deng 0 7月 13 21:42 d [deng@localhost test]$ cp -r dir dir1 [deng@localhost test]$ ls -l dir1 總用量 0 -rw-rw-r-- 1 deng deng 0 7月 13 21:56 a lrwxrwxrwx 1 deng deng 1 7月 13 21:56 a_link -> a lrwxrwxrwx 1 deng deng 1 7月 13 21:56 a_link2 -> a -rw-rw-r-- 1 deng deng 0 7月 13 21:56 a_link3 -rw-rw-r-- 1 deng deng 0 7月 13 21:56 b -rw-rw-r-- 1 deng deng 0 7月 13 21:56 c -rw-rw-r-- 1 deng deng 0 7月 13 21:56 d [deng@localhost test]$4.10 詳細(xì)顯示命令執(zhí)行的操作
默認(rèn)情況下,當(dāng)拷貝成功時(shí),僅會(huì)再次看到命令提示符。如果想了解拷貝過(guò)程,可以用 -v 選項(xiàng)。
[deng@localhost test]$ cp -v b c d dir1/ "b" -> "dir1/b" "c" -> "dir1/c" "d" -> "dir1/d" [deng@localhost test]$4.11 覆蓋前詢(xún)問(wèn)(使前面的 -n 選項(xiàng)失效)
[deng@localhost test]$ cp -i b dir1/ cp:是否覆蓋"dir1/b"? y [deng@localhost test]$4.12 蓋已存在的文件目標(biāo)前將目標(biāo)文件備份
當(dāng)目標(biāo)目錄已經(jīng)含有同名文件,默認(rèn)情況下 cp 命令會(huì)覆蓋目標(biāo)目錄下的同名文件。使用 --backup 選項(xiàng),cp 命令會(huì)為每一個(gè)現(xiàn)有的目標(biāo)文件做一個(gè)備份。
[deng@localhost test]$ cp -bv b dir1/ "b" -> "dir1/b" (備份:"dir1/b~") [deng@localhost test]$ ls -l dir1/ 總用量 0 -rw-rw-r-- 1 deng deng 0 7月 13 22:02 b -rw-rw-r-- 1 deng deng 0 7月 13 22:00 b~ -rw-rw-r-- 1 deng deng 0 7月 13 21:59 c -rw-rw-r-- 1 deng deng 0 7月 13 21:59 d [deng@localhost test]$波浪符號(hào)(~)意味著文件是備份文件。從屬性中可以看到,備份b~文件比b 要舊。
4.13 只拷貝文件屬性,不拷貝文件內(nèi)容
選項(xiàng) --attributes-only,只會(huì)拷貝文件名及其屬性,不會(huì)拷貝任何數(shù)據(jù)。
[deng@localhost test]$ cp --attributes-only /etc/passwd passwd [deng@localhost test]$ ls -l /etc/passwd passwd -rw-r--r-- 1 root root 2376 7月 12 17:32 /etc/passwd -rw-r--r-- 1 deng deng 0 7月 13 22:04 passwd [deng@localhost test]$拷貝的文件只有 0 字節(jié)大小,這是因?yàn)槲募?nèi)容并沒(méi)有拷貝。
4.14 強(qiáng)制拷貝文件
使用 -f 選項(xiàng)會(huì)強(qiáng)制進(jìn)行拷貝操作。如果目標(biāo)文件不能打開(kāi),可以用 -f 嘗試。
[deng@localhost test]$ cp -fv /etc/passwd passwd "/etc/passwd" -> "passwd" [deng@localhost test]$05. 附錄
參考:【Linux】一步一步學(xué)Linux系列教程匯總
總結(jié)
以上是生活随笔為你收集整理的【Linux】一步一步学Linux——cp命令(31)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Linux】一步一步学Linux——m
- 下一篇: 【Linux】一步一步学Linux——r