linux命令:rm命令學習與使用,linux命令rm學習

來源:互聯網
上載者:User

linux命令:rm命令學習與使用,linux命令rm學習

昨天學習了建立檔案和目錄的命令mkdir,今天學習一下linux中刪除檔案和目錄的命令:rm命令。rm是常用的命令,該命令的功能為刪除一個目錄中的一個或多個檔案或目錄,它也可以將某個目錄及其下的所有檔案及子目錄均刪除。對於連結檔案,只是刪除了連結,原有檔案均保持不變。

rm是一個危險的命令,使用的時候要特別當心,尤其對於新手,否則整個系統就會毀在這個命令(比如在/(根目錄)下執行rm-rf)。所以,我們在執行rm之前最好先確認一下在哪個目錄,到底要刪除什麼東西,操作時保持高度清醒的頭腦。

1.命令格式:

rm[選項]檔案…

2.命令功能:

刪除一個目錄中的一個或多個檔案或目錄,如果沒有使用-r選項,則rm不會刪除目錄。如果使用rm來刪除檔案,通常仍可以將該檔案恢複原狀。

3.命令參數:

-f,–force忽略不存在的檔案,從不給出提示。

-i,–interactive進行互動式刪除

-r,-R,–recursive指示rm將參數中列出的全部目錄和子目錄均遞迴地刪除。

-v,–verbose詳細顯示進行的步驟

–help顯示此協助資訊並退出

–version輸出版本資訊並退出

4.命令執行個體:

執行個體一:刪除檔案file,系統會先詢問是否刪除。

命令:

rm檔案名稱

輸出:

[root@localhosttest1]#ll

總計4

-rw-r–r–1rootroot5610-2614:31log.log

root@localhosttest1]#rmlog.log

rm:是否刪除一般檔案“log.log”?y

root@localhosttest1]#ll

總計0[root@localhosttest1]#

說明:

輸入rmlog.log命令後,系統會詢問是否刪除,輸入y後就會刪除檔案,不想刪除則資料n。

執行個體二:強行刪除file,系統不再提示。

命令:

rm-flog1.log

輸出:

[root@localhosttest1]#ll

總計4

-rw-r–r–1rootroot2310-2614:40log1.log

[root@localhosttest1]#rm-flog1.log

[root@localhosttest1]#ll

總計0[root@localhosttest1]#

執行個體三:刪除任何.log檔案;刪除前逐一詢問確認

命令:

rm-i.log

輸出:

[root@localhosttest1]#ll

總計8

-rw-r–r–1rootroot1110-2614:45log1.log

-rw-r–r–1rootroot2410-2614:45log2.log

[root@localhosttest1]#rm-i*.log

rm:是否刪除一般檔案“log1.log”?y

rm:是否刪除一般檔案“log2.log”?y

[root@localhosttest1]#ll

總計0[root@localhosttest1]#

執行個體四:將test1子目錄及子目錄中所有檔案刪除

命令:

rm-rtest1

輸出:

[root@localhosttest]#ll

總計24drwxr-xr-x7rootroot409610-2518:07scf

drwxr-xr-x2rootroot409610-2614:51test1

drwxr-xr-x3rootroot409610-2517:44test2

drwxrwxrwx2rootroot409610-2517:46test3

drwxr-xr-x2rootroot409610-2517:56test4

drwxr-xr-x3rootroot409610-2517:56test5

[root@localhosttest]#rm-rtest1

rm:是否進入目錄“test1”?y

rm:是否刪除一般檔案“test1/log3.log”?y

rm:是否刪除目錄“test1”?y

[root@localhosttest]#ll

總計20drwxr-xr-x7rootroot409610-2518:07scf

drwxr-xr-x3rootroot409610-2517:44test2

drwxrwxrwx2rootroot409610-2517:46test3

drwxr-xr-x2rootroot409610-2517:56test4

drwxr-xr-x3rootroot409610-2517:56test5

[root@localhosttest]#

執行個體五:rm-rftest2命令會將test2子目錄及子目錄中所有檔案刪除,並且不用一一確認

命令:

rm-rftest2

輸出:

[root@localhosttest]#rm-rftest2

[root@localhosttest]#ll

總計16drwxr-xr-x7rootroot409610-2518:07scf

drwxrwxrwx2rootroot409610-2517:46test3

drwxr-xr-x2rootroot409610-2517:56test4

drwxr-xr-x3rootroot409610-2517:56test5

[root@localhosttest]#

執行個體六:刪除以-f開頭的檔案

命令:

rm–-f

輸出:

[root@localhosttest]#touch–-f

[root@localhosttest]#ls–-f

-f[root@localhosttest]#rm–-f

rm:是否刪除一般空檔案“-f”?y

[root@localhosttest]#ls–-f

ls:-f:沒有那個檔案或目錄

[root@localhosttest]#

也可以使用下面的操作步驟:

[root@localhosttest]#touch./-f

[root@localhosttest]#ls./-f

./-f[root@localhosttest]#rm./-f

rm:是否刪除一般空檔案“./-f”?y

[root@localhosttest]#

執行個體七:自訂資源回收筒功能

命令:

myrm(){D=/tmp/(date +%Y%m%d%H%M%S); mkdir -p D;mv”@" D&&echo“movedtoD ok"; }

輸出:

[root@localhost test]# myrm(){ D=/tmp/(date +%Y%m%d%H%M%S); mkdir -p D; mv "@" D && echo "moved to Dok”;}

[root@localhosttest]#aliasrm=’myrm’

[root@localhosttest]#touch1.log2.log3.log

[root@localhosttest]#ll

總計16

-rw-r–r–1rootroot010-2615:081.log

-rw-r–r–1rootroot010-2615:082.log

-rw-r–r–1rootroot010-2615:083.log

drwxr-xr-x7rootroot409610-2518:07scf

drwxrwxrwx2rootroot409610-2517:46test3

drwxr-xr-x2rootroot409610-2517:56test4

drwxr-xr-x3rootroot409610-2517:56test5

[root@localhosttest]#rm[123].log

movedto/tmp/20121026150901ok

[root@localhosttest]#ll

總計16drwxr-xr-x7rootroot409610-2518:07scf

drwxrwxrwx2rootroot409610-2517:46test3

drwxr-xr-x2rootroot409610-2517:56test4

drwxr-xr-x3rootroot409610-2517:56test5

[root@localhosttest]#ls/tmp/20121026150901/

1.log2.log3.log

[root@localhosttest]#

說明:

上面的操作過程類比了資源回收筒的效果,即刪除檔案的時候只是把檔案放到一個臨時目錄中,這樣在需要的時候還可以恢複過來。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.