21天掌握linux常用命令挑戰開啟了!今天我們將深入探討cp命令的用法和功能。
-
命令簡介 cp命令主要用于復制文件或文件夾。你可以將一個或多個源文件或文件夾復制到指定的目標文件或文件夾中,這類似于在windows系統中復制粘貼文件或文件夾。
-
英文含義記憶法
cp = copy
-
語法格式
cp 選項... -T 源文件 目標文件 cp 選項... 源文件... 目錄 cp 選項... -t 目錄 源文件...
-
選項說明
- -r: 遞歸復制文件夾及其內容。
- -i: 在覆蓋目標文件前提示用戶確認,輸入y確認覆蓋。
- -f: 強制覆蓋目標文件,無需確認。
- -a: 保持原樣復制,包括時間、訪問權限和文件所有者等屬性。
- -p: 保留源文件或目錄的屬性。
- -l: 不復制文件,僅生成鏈接文件。
- -v: 詳細顯示命令執行的操作。
-
注意事項
- 源文件:默認情況下,cp命令無法復制文件夾,必須使用-R選項。
- 目標文件:當源文件為多個時,目標文件必須是指定的目錄。
-
示例說明
6.1 將一個文件夾及文件夾下所有內容拷貝到另外一個文件夾
[root@iZbp1d8rn0652ia3bzzmioZ local]# cd test1/ [root@iZbp1d8rn0652ia3bzzmioZ test1]# ls -l 總用量 0 -rw-r--r-- 1 root root 0 11月 23 23:00 111111.txt -rw-r--r-- 1 root root 0 11月 23 23:00 1111.txt -rw-r--r-- 1 root root 0 11月 23 22:59 111.txt [root@iZ test1]# cd ../test2/ [root@iZ test2]# ls [root@iZ test2]# cp -r /usr/local/test1/ /usr/local/test2/ [root@iZ test2]# ls test1 [root@iZ test2]# cd test1/ [root@iZ test1]# ls 111111.txt 1111.txt 111.txt [root@iZ test1]#
6.2 將一個文件夾下所有內容拷貝到另外一個文件夾下
drwxr-xr-x 2 root root 55 11月 23 23:00 test1 drwxr-xr-x 2 root root 6 11月 23 23:07 test2 [root@iZ local]# cd test1/ [root@iZ test1]# ls 111111.txt 1111.txt 111.txt [root@iZ test1]# cd ../test2/ [root@iZ test2]# ls [root@iZ test2]# cp /usr/local/test1/* /usr/local/test2 [root@iZ test2]# ls 111111.txt 1111.txt 111.txt [root@iZ test2]#
6.3 目標文件下已經存在復制文件時,在覆蓋時會先詢問是否執行操作
[root@iZ test2]# ls 111111.txt 1111.txt 111.txt [root@iZ test2]# cd ../test1/ [root@iZ test1]# ls 111111.txt 1111.txt 111.txt [root@iZ test1]# cp -i 111.txt /usr/local/test2 cp:是否覆蓋'/usr/local/test2/111.txt'? y [root@iZ test1]#
6.4 原樣復制,保留全部
[root@iZ test1]# ls -l 總用量 0 -rw-r--r-- 1 root root 0 11月 23 23:00 111111.txt -rw-r--r-- 1 root root 0 11月 23 23:00 1111.txt -rw-r--r-- 1 root root 0 11月 23 22:59 111.txt [root@iZ test1]# cp 111.txt /usr/local/test2/ [root@iZ test1]# cd /usr/local/test2/ [root@iZ test2]# ls -l 總用量 0 -rw-r--r-- 1 root root 0 11月 23 23:28 111.txt
[root@iZ test1]# ls -l 總用量 0 -rw-r--r-- 1 root root 0 11月 23 23:00 111111.txt -rw-r--r-- 1 root root 0 11月 23 23:00 1111.txt -rw-r--r-- 1 root root 0 11月 23 22:59 111.txt [root@iZ test1]# cp -a 1111.txt /usr/local/test2/ [root@iZ test1]# cd /usr/local/test2/ [root@iZ test2]# ls -l 總用量 0 -rw-r--r-- 1 root root 0 11月 23 23:00 1111.txt -rw-r--r-- 1 root root 0 11月 23 23:28 111.txt
今天的學習內容就到這里了!歡迎大家在評論區參與交流與討論,一起學習和進步!
系列推薦 日更第1天:linux常用命令之dnf用法
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END