cp 命令詳解
歡迎來到藍(lán)隊(duì)云技術(shù)小課堂。
cp命令是Linux系統(tǒng)中用于復(fù)制文件和目錄的核心命令。其基本語法為cp [選項(xiàng)] 源文件 目標(biāo)文件或cp [選項(xiàng)] 源文件... 目標(biāo)目錄。
常用選項(xiàng):
· -r或-R:遞歸復(fù)制目錄
· -i:覆蓋前詢問確認(rèn)
· -v:顯示復(fù)制過程
· -u:只復(fù)制更新的文件
· -p:保留文件屬性(權(quán)限、時(shí)間戳等)
· -a:歸檔模式(保留所有屬性,相當(dāng)于-dpR)
# 基本復(fù)制
cp file1.txt file2.txt # 復(fù)制文件
cp file1.txt /path/to/destination/ # 復(fù)制到目錄
cp file1.txt file2.txt backup/ # 復(fù)制多個(gè)文件
cp *.txt documents/ # 復(fù)制所有txt文件
# 遞歸復(fù)制目錄
cp -r dir1 dir2 # 遞歸復(fù)制目錄
cp -R dir1 dir2 # 同上
cp -a dir1 dir2 # 歸檔模式(保留所有屬性)
cp -r dir1/. dir2/ # 復(fù)制目錄內(nèi)容(不包括目錄本身)
# 保留屬性
cp -p file1.txt file2.txt # 保留權(quán)限、時(shí)間戳等
cp --preserve=all file1.txt file2.txt # 保留所有屬性
cp -u file1.txt file2.txt # 只有源文件更新才復(fù)制
cp -b file1.txt file2.txt # 覆蓋前創(chuàng)建備份
cp --backup=numbered file1.txt file2.txt # 創(chuàng)建帶編號(hào)的備份
# 交互和安全
cp -i file1.txt file2.txt # 覆蓋前詢問
cp -n file1.txt file2.txt # 不覆蓋已存在文件
cp -v file1.txt file2.txt # 顯示復(fù)制過程
cp -f file1.txt file2.txt # 強(qiáng)制復(fù)制
# 鏈接處理
cp -d symlink.txt copy.txt # 保留符號(hào)鏈接
cp -L symlink.txt copy.txt # 跟隨符號(hào)鏈接
cp -l file1.txt file2.txt # 創(chuàng)建硬鏈接而非復(fù)制
cp -s file1.txt file2.txt # 創(chuàng)建符號(hào)鏈接而非復(fù)制
# 實(shí)用示例
cp -r /home/user/documents /backup/ # 備份目錄
cp -p /etc/passwd /backup/ # 備份重要文件
cp -u *.txt /backup/ # 只復(fù)制更新的文件
藍(lán)隊(duì)云官網(wǎng)上擁有完善的技術(shù)支持庫(kù)可供參考,大家可自行查閱,更多技術(shù)問題,可以直接咨詢。同時(shí),藍(lán)隊(duì)云整理了運(yùn)維必備的工具包免費(fèi)分享給大家使用,需要的朋友可以直接咨詢。
更多技術(shù)知識(shí),藍(lán)隊(duì)云期待與你一起探索。