1、Linux上的文件管理類命令都有哪些?其常用的使用方法及其相關示例演示。
文件管理類命令:cp、mv、rm
1.1 cp命令
cp – copy files and directories
復制文件或目錄
【SYNOPSIS】
單源復制:cp [OPTION]… [-T] SOURCE DEST
多源復制:cp [OPTION]… SOURCE(源文件)… DIRECTORY(目錄)
cp [OPTION]… -t DIRECTORY SOURCE…
單源復制:cp [OPTION]… [-T] SOURCE DEST
如果DEST(目標)不存在:則事先創建些文件,并復制源文件的數據流至DEST中;
如果DEST存在:
如果DEST是非目錄文件;則覆蓋目錄文件;
如果DEST是目錄文件;則先在DEST目錄下創建一個與源文件同名的文件,并復制其數據流至目標文件;
多源復制:cp [OPTION]… SOURCE… DIRECTORY
cp [OPTION]… -t DIRECTORY SOURCE…
如果DEST不存在:報錯;
如果DEST存在:
如果DEST是非目錄文件:報錯;
如果DEST是目錄文件:分別復制每個文件至目標目錄中,并保持原名;
【OPTIONS】
-i:交互式復制,即覆蓋之前提醒用戶確認;※
-f:強制覆蓋目標文件,而不提示;
-r:-R:遞歸復制目錄;
-d:復制符號鏈接文件本身,而非其指向的源文件;
-p:復制源文件內容后,還將把其修改時間和訪問權限也復制到新文件中;※
-P:源文件或者文件的路徑保留;
-b:或–backup,刪除、覆蓋目標文件之前先備份,備份的文件會在字尾加上一個備份字符串;
-s:不進行復制成,而是建立符號鏈接;
-S <備份字尾字符串>:或用–suffix=<備份字尾字符串>,用“-b”參數備份目標文件后,備份文件的字尾會加上一個備份字符串。默認的備份字符串是符號“~”,可通過“S”來改變它。
-u:只有在源文件的改變時間比目標文件更新的時候,或是名稱相互對應的目標文件不存在時,才復制文件;
-l:對源文件建立硬鏈接,而非復制;
-v:執行時顯示詳細過程;
-a:-dR –preserve=all,archive,用于實現歸檔,保留鏈接和文件屬性,遞歸拷貝目錄,相當于下面的d、 –preserv=
mode:權限
ownership:屬主屬組
timestamps:時間戳
contest:安全標簽
xattr:擴展屬性
links:符號鏈接
all:上述所有屬性
【EXAMPLES】
實例1:直接執行cp命令,不帶任何參數
[root@zck ~]# cd /test/ [root@zck test]# ll /var/log/wtmp -rw-rw-r--. 1 root utmp 67200 Aug 15 18:35 /var/log/wtmp [root@zck test]# cp /var/log/wtmp . #"."表示復制到當前目錄 [root@zck test]# ll total 68 -rw-r--r--. 1 root root 67200 Aug 15 22:26 wtmp #查看標注,在不加任何選項的情況下,文件的某些屬性/權限會改變;連文件建立的時間也不一樣。
實例2:執行cp -a連源文件屬性全部復制。
[root@zck test]# ll /var/log/wtmp -rw-rw-r--. 1 root utmp 68736 Aug 16 08:57 /var/log/wtmp [root@zck test]# cp -a /var/log/wtmp wtmp_2 [root@zck test]# ll total 68 -rw-rw-r--. 1 root utmp 68736 Aug 16 08:57 wtmp_2 #查看標注,復制過來的屬性與源文件一模一樣。
實例3:使用交互式復制將wtmp復制成wtmp2
[root@zck test]# ll total 68 -rw-rw-r--. 1 root utmp 68736 Aug 16 08:57 wtmp_2 [root@zck test]# cp -i /var/log/wtmp wtmp_2 cp: overwrite ‘wtmp_2’? y #提示是否覆蓋‘wtmp_2’ [root@zck test]# ll total 68 -rw-rw-r--. 1 root utmp 68736 Aug 16 18:37 wtmp_2
實例4:使用強制復制將wtmp復制成wtmp2
[root@zck test]# cp -f /var/log/wtmp wtmp_2 cp: overwrite ‘wtmp_2’? y #使用強制復制,仍會提示用戶確認,是由于cp命令默認帶-i參數。 [root@zck test]# ll total 68 -rw-rw-r--. 1 root utmp 68736 Aug 16 18:48 wtmp_2 [root@zck test]# alias alias cls='clear'alias cp='cp -i' #cp命令默認設置了命令別名帶-i參數。
實例5:單源復制,將目錄/tmp/man/復制成dir1,目錄之間的復制將因目標目錄的存在與否而有所差異;
[root@zck test]# cp -R /tmp/man/ dir1 [root@zck test]# ll total 4 drwxr-xr-x. 11 root root 4096 Aug 16 19:28 dir1 [root@zck test]# ll dir1/ total 360 drwxr-xr-x. 2 root root 53248 Aug 16 19:28 man1 drwxr-xr-x. 2 root root 12288 Aug 16 19:28 man2 drwxr-xr-x. 2 root root 110592 Aug 16 19:28 man3 drwxr-xr-x. 2 root root 4096 Aug 16 19:28 man4 drwxr-xr-x. 2 root root 16384 Aug 16 19:28 man5 drwxr-xr-x. 2 root root 23 Aug 16 19:28 man6 drwxr-xr-x. 2 root root 8192 Aug 16 19:28 man7 drwxr-xr-x. 2 root root 32768 Aug 16 19:28 man8 drwxr-xr-x. 2 root root 6 Aug 16 19:28 man9 #由于dir1目錄不存在,所以會建立dir1這個目錄,并把/tmp/man/目錄下的文件與子目錄都復制到dir1中。 [root@zck test]# cp -R /tmp/man/ dir1 [root@zck test]# ll dir1 total 364drwxr-xr-x. 11 root root 4096 Aug 16 20:08 man drwxr-xr-x. 2 root root 53248 Aug 16 19:28 man1 drwxr-xr-x. 2 root root 12288 Aug 16 19:28 man2 drwxr-xr-x. 2 root root 110592 Aug 16 19:28 man3 drwxr-xr-x. 2 root root 4096 Aug 16 19:28 man4 drwxr-xr-x. 2 root root 16384 Aug 16 19:28 man5 drwxr-xr-x. 2 root root 23 Aug 16 19:28 man6 drwxr-xr-x. 2 root root 8192 Aug 16 19:28 man7 drwxr-xr-x. 2 root root 32768 Aug 16 19:28 man8 drwxr-xr-x. 2 root root 6 Aug 16 19:28 man9 #由于dir1已經存在,則是把整個/tmp/man/目錄,即man目錄本身及目錄下的所有文件與子目錄都復制到dir1中,即dir1/man中。
實例6:多源復制,將file1、file2、file3、dir1復制到dir3目錄下
[root@zck test]# cp -r file1 file2 file3 dir1 dir3 cp: target ‘dir3’ is not a directory #由于目標目錄dir3不存在,所以會報錯 [root@zck test]# mkdir dir3 #創建dir3目錄 [root@zck test]# cp -r file1 file2 file3 dir1 dir3 [root@zck test]# ll dir3 total 364 drwxr-xr-x. 12 root root 4096 Aug 16 20:27 dir1 -rw-r--r--. 1 root root 0 Aug 16 20:27 file1 -rw-r--r--. 1 root root 0 Aug 16 20:27 file2 -rw-r--r--. 1 root root 0 Aug 16 20:27 file3 #由于目標目錄dir3已經存在分別將file1 file2 file3 dir1復制到目標目錄中,并保持原名。
實例7:只有源文件的改變時間較新時,才復制文件
[root@zck test]# ll total 2 -rw-r--r--. 1 root root 0 Aug 16 20:26 file1 -rw-r--r--. 1 root root 0 Aug 16 20:25 file2 [root@zck test]# cp -u -v file1 file2 cp: overwrite ‘file2’? y #由于file1的改變時間比file2文件的改變時間較新才會執行復制。 ‘file1’ -> ‘file2’ #-v,顯示執行詳細過程。 [root@zck test]# ll total 2 -rw-r--r--. 1 root root 0 Aug 16 20:26 file1 -rw-r--r--. 1 root root 0 Aug 16 21:06 file2 [root@zck test]# cp -u -v file1 file2 #由于file1的改變時間比file2文件的改變時間舊,擬不會執行復制。 總結:cp命令的-u參數,是在目標文件與來源文件有差異時,才會復制,否則不會復制,所以,比較常用于備份工作當中。
實例8:-b參數,file2是已經存在的文件,將file1復制成file2時,則是生成一個備份文件;
[root@zck test]# cp -b file1 file2 cp: overwrite ‘file2’? y [root@zck test]# ll total 3 -rw-r--r--. 1 root root 0 Aug 16 20:26 file1 -rw-r--r--. 1 root root 0 Aug 16 21:43 file2 -rw-r--r--. 1 root root 0 Aug 16 21:06 file2~ [root@zck test]# cp -b -S _backup file1 file2 #-S,指定備份字尾字符串 cp: overwrite ‘file2’? y [root@zck test]# ll total 4 -rw-r--r--. 1 root root 0 Aug 16 20:26 file1 -rw-r--r--. 1 root root 0 Aug 16 21:44 file2 -rw-r--r--. 1 root root 0 Aug 16 21:06 file2~ -rw-r--r--. 1 root root 0 Aug 16 21:43 file2_backup
實例9:復制文件時,建立源文件的硬鏈接或軟件鏈接,分別用-l、-s參數;
[root@zck test]# cp -l file1 file2 #建立源文件硬鏈接,而非復制 [root@zck test]# ll total 8 -rw-r--r--. 2 root root 6 Aug 16 21:49 file1 -rw-r--r--. 2 root root 6 Aug 16 21:49 file2 -rw-r--r--. 1 root root 0 Aug 16 20:25 file3 [root@zck test]# cat file2 file1 [root@zck test]# cp -s file1 file4 #建立源文件軟鏈接,而非復制 [root@zck test]# ll total 8 -rw-r--r--. 2 root root 6 Aug 16 21:49 file1 -rw-r--r--. 2 root root 6 Aug 16 21:49 file2 -rw-r--r--. 1 root root 0 Aug 16 20:25 file3 lrwxrwxrwx. 1 root root 5 Aug 16 22:05 file4 -> file1 [root@zck test]# echo "file2" >> file2 [root@zck test]# cat file1 file1 file2 [root@zck test]# echo "file4" >> file4 [root@zck test]# cat file1 file1 file2 file4
實例10:執行cp -d,復制一個鏈接文件
[root@zck log_link]# echo "link" >log_link [root@zck log_link]# cat log_link link [root@zck log_link]# ll total 4 -rw-r--r--. 1 root root 5 Aug 16 22:22 log_link [root@zck log_link]# cp -s log_link log_slink [root@zck log_link]# ll total 4 -rw-r--r--. 1 root root 5 Aug 16 22:22 log_link lrwxrwxrwx. 1 root root 8 Aug 16 22:22 log_slink -> log_link [root@zck log_link]# cp log_slink log_slink1 [root@zck log_link]# cp -d log_slink log_slink2 [root@zck log_link]# ll total 8 -rw-r--r--. 1 root root 5 Aug 16 22:22 log_link lrwxrwxrwx. 1 root root 8 Aug 16 22:22 log_slink -> log_link -rw-r--r--. 1 root root 5 Aug 16 22:23 log_slink1 #與源文件相同 lrwxrwxrwx. 1 root root 8 Aug 16 22:23 log_slink2 -> log_link #是鏈接文件 總結:如果cp命令不加上任何參數,cp復制的是源文件,而非鏈接文件的屬性。若要復制鏈接文件的屬性,就得使用-d的參數。
1.3 rm 命令
rm – remove files or directories
刪除文件或目錄
【SYNOPSIS】
rm [OPTION]… FILE…
【OPTIONS】
-d:直接把要刪除的目錄的硬鏈接數刪成0,并移除該目錄;
-i:交互式刪除,刪除已有文件或目錄之前先詢問用戶;
-f:–force(強制),強制刪除文件或目錄;
-r、-R:遞歸處理,將指定目錄下的所有文件和子目錄一并刪除;
-v:顯示命令的執行過程;
【EXAMPLES】
實例1:執行rm命令,不帶任何參數
[root@zck test]# tree . ├── dir1 ├── dir2 ├── dir3 ├── file1.txt ├── file2.txt └── file3.txt 3 directories, 3 files [root@zck test]# rm file1.txt rm: remove regular empty file ‘file1.txt’? y #詢問是否刪除,不想刪除輸入“n” [root@zck test]# ll file1 ls: cannot access file1: No such file or directory #找不到此文件,已經刪除 #linux默認設置了別名rm -i參數,所以執行rm,會默認詢問是否刪除 [root@zck test]# alias | grep rm alias rm='rm -i'
實例2:執行rm命令,帶-f參數
[root@zck test]# rm -f file2.txt [root@zck test]# ll file2.txt ls: cannot access file2.txt: No such file or directory
實例3:執行rm命令,帶-i參數
[root@zck test]# rm -i *.txt rm: remove regular empty file ‘file3.txt’? y [root@zck test]# ll total 0 #刪除所有.txt的文件,刪除前逐一詢問是否刪除。
實例 4:執行 rm 命令,帶-r 參數,將test1子目錄及子目錄中所有檔案刪除。
[root@zck test]# mkdir -vp /tmp/test/{test1,test2,test3,test4/txt} mkdir: created directory ‘/tmp/test/test1’ mkdir: created directory ‘/tmp/test/test2’ mkdir: created directory ‘/tmp/test/test3’ mkdir: created directory ‘/tmp/test/test4’ mkdir: created directory ‘/tmp/test/test4/txt’ [root@zck test]# ll total 0 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test1 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test2 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test3 drwxr-xr-x. 3 root root 16 Aug 17 18:34 test4 [root@zck test]# rm -r test1 rm: remove directory ‘test1’? y [root@zck test]# ll total 0 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test2 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test3 drwxr-xr-x. 3 root root 16 Aug 17 18:34 test4 [root@zck test]# rm -rf test2 [root@zck test]# ll total 0 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test3 drwxr-xr-x. 3 root root 16 Aug 17 18:34 test4
實例 6:自定義回收站功能
下面的操作過程模擬了回收站的效果,即刪除文件的時候只是把文件放到一個臨時目錄中,這樣在需要的時候還可以恢復過來。
命令:
myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
[root@zck test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; } [root@zck test]# alias rm='myrm' [root@zck test]# touch 1.log 2.log 3.log [root@zck test]# ll total 0 -rw-r--r--. 1 root root 0 Aug 17 18:40 1.log -rw-r--r--. 1 root root 0 Aug 17 18:40 2.log -rw-r--r--. 1 root root 0 Aug 17 18:40 3.log drwxr-xr-x. 2 root root 6 Aug 17 18:34 test3 drwxr-xr-x. 3 root root 16 Aug 17 18:34 test4 [root@zck test]# rm [123].log moved to /tmp/20160817184051 ok [root@zck test]# ll total 0 drwxr-xr-x. 2 root root 6 Aug 17 18:34 test3 drwxr-xr-x. 3 root root 16 Aug 17 18:34 test4 [root@zck test]# ls /tmp/20160817184051/ 1.log 2.log 3.log
原創文章,作者:zhuckee,如若轉載,請注明出處:http://www.www58058.com/36217
對于有錯誤提示的都寫出了原因,很不錯,繼續保持。