每天一個linux命令(5):rm命令

來源:互聯網
上載者:User


每天一個linux命令(5):rm命令 相關連結:每天一個linux命令(1):ls命令http://www.bkjia.com/os/201210/163049.html;每天一個linux命令(2):cd命令http://www.bkjia.com/os/201210/163050.html;每天一個linux命令(3):pwd命令http://www.bkjia.com/os/201210/163462.html;每天一個linux命令(4):mkdir命令http://www.bkjia.com/os/201210/163463.html 昨天學習了建立檔案和目錄的命令mkdir ,今天學習一下linux中刪除檔案和目錄的命令: rm命令。rm是常用的命令,該命令的功能為刪除一個目錄中的一個或多個檔案或目錄,它也可以將某個目錄及其下的所有檔案及子目錄均刪除。對於連結檔案,只是刪除了連結,原有檔案均保持不變。  www.2cto.com  rm是一個危險的命令,使用的時候要特別當心,尤其對於新手,否則整個系統就會毀在這個命令(比如在/(根目錄)下執行rm * -rf)。所以,我們在執行rm之前最好先確認一下在哪個目錄,到底要刪除什麼東西,操作時保持高度清醒的頭腦。1.命令格式:rm [選項] 檔案… 2.命令功能:刪除一個目錄中的一個或多個檔案或目錄,如果沒有使用- r選項,則rm不會刪除目錄。如果使用 rm 來刪除檔案,通常仍可以將該檔案恢複原狀。3.命令參數:    -f, --force    忽略不存在的檔案,從不給出提示。    -i, --interactive 進行互動式刪除    -r, -R, --recursive   指示rm將參數中列出的全部目錄和子目錄均遞迴地刪除。    -v, --verbose    詳細顯示進行的步驟      --help     顯示此協助資訊並退出      --version  輸出版本資訊並退出  www.2cto.com  4.命令執行個體:執行個體一:刪除檔案file,系統會先詢問是否刪除。 命令:rm 檔案名稱輸出:[root@localhost test1]# ll總計 4-rw-r--r-- 1 root root 56 10-26 14:31 log.logroot@localhost test1]# rm log.log rm:是否刪除 一般檔案 “log.log”? yroot@localhost test1]# ll總計 0[root@localhost test1]#說明:輸入rm log.log命令後,系統會詢問是否刪除,輸入y後就會刪除檔案,不想刪除則資料n。  www.2cto.com  執行個體二:強行刪除file,系統不再提示。 命令:rm -f log1.log輸出:[root@localhost test1]# ll總計 4-rw-r--r-- 1 root root 23 10-26 14:40 log1.log[root@localhost test1]# rm -f log1.log [root@localhost test1]# ll總計 0[root@localhost test1]#執行個體三:刪除任何.log檔案;刪除前逐一詢問確認 命令:rm -i *.log輸出:[root@localhost test1]# ll總計 8-rw-r--r-- 1 root root 11 10-26 14:45 log1.log-rw-r--r-- 1 root root 24 10-26 14:45 log2.log[root@localhost test1]# rm -i *.logrm:是否刪除 一般檔案 “log1.log”? yrm:是否刪除 一般檔案 “log2.log”? y[root@localhost test1]# ll總計 0[root@localhost test1]#執行個體四:將 test1子目錄及子目錄中所有檔案刪除命令:rm -r test1輸出:[root@localhost test]# ll總計 24drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxr-xr-x 2 root root 4096 10-26 14:51 test1drwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@localhost test]# rm -r test1rm:是否進入目錄 “test1”? yrm:是否刪除 一般檔案 “test1/log3.log”? yrm:是否刪除 目錄 “test1”? y[root@localhost test]# ll總計 20drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxr-xr-x 3 root root 4096 10-25 17:44 test2drwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@localhost test]#執行個體五:rm -rf test2命令會將 test2 子目錄及子目錄中所有檔案刪除,並且不用一一確認命令:rm -rf  test2 輸出:[root@localhost test]# rm -rf test2[root@localhost test]# ll總計 16drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@localhost test]#  www.2cto.com  執行個體六:刪除以 -f 開頭的檔案命令:rm -- -f輸出:[root@localhost test]# touch -- -f[root@localhost test]# ls -- -f-f[root@localhost test]# rm -- -frm:是否刪除 一般空檔案 “-f”? y[root@localhost test]# ls -- -fls: -f: 沒有那個檔案或目錄[root@localhost test]#也可以使用下面的操作步驟:[root@localhost test]# touch ./-f[root@localhost test]# ls ./-f./-f[root@localhost test]# rm ./-frm:是否刪除 一般空檔案 “./-f”? y[root@localhost test]#  www.2cto.com  執行個體七:自訂資源回收筒功能命令:myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }輸出:[root@localhost test]# myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }[root@localhost test]# alias rm='myrm'[root@localhost test]# touch 1.log 2.log 3.log[root@localhost test]# ll總計 16-rw-r--r-- 1 root root    0 10-26 15:08 1.log-rw-r--r-- 1 root root    0 10-26 15:08 2.log-rw-r--r-- 1 root root    0 10-26 15:08 3.logdrwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@localhost test]# rm [123].logmoved to /tmp/20121026150901 ok[root@localhost test]# ll總計 16drwxr-xr-x 7 root root 4096 10-25 18:07 scfdrwxrwxrwx 2 root root 4096 10-25 17:46 test3drwxr-xr-x 2 root root 4096 10-25 17:56 test4drwxr-xr-x 3 root root 4096 10-25 17:56 test5[root@localhost test]# ls /tmp/20121026150901/1.log  2.log  3.log[root@localhost test]#說明:上面的操作過程類比了資源回收筒的效果,即刪除檔案的時候只是把檔案放到一個臨時目錄中,這樣在需要的時候還可以恢複過來。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.