linuxrm命令刪除文件和目錄使用詳解
rm是常用的命令,該命令的功能為刪除一個目錄中的一個或多個文件或目錄,它也可以將某個目錄及其下的所有文件及子目錄均刪除。對于鏈接文件,只是刪除了鏈接,原有文件均保持不變。
rm是一個危險的命令,使用的時候要特別當(dāng)心,尤其對于新手,否則整個系統(tǒng)就會毀在這個命令(比如在/(根目錄)下執(zhí)行rm * -rf)。所以,我們在執(zhí)行rm之前最好先確認一下在哪個目錄,到底要刪除什么東西,操作時保持高度清醒的頭腦。
1.命令格式:
rm [選項] 文件…
2.命令功能:
刪除一個目錄中的一個或多個文件或目錄,如果沒有使用- r選項,則rm不會刪除目錄。如果使用 rm 來刪除文件,通常仍可以將該文件恢復(fù)原狀。
3.命令參數(shù):
-f, --force 忽略不存在的文件,從不給出提示。
-i, --interactive 進行交互式刪除
-r, -R, --recursive 指示rm將參數(shù)中列出的全部目錄和子目錄均遞歸地刪除。
-v, --verbose 詳細顯示進行的步驟
--help 顯示此幫助信息并退出
--version 輸出版本信息并退出
4.命令實例:
實例一:刪除文件file,系統(tǒng)會先詢問是否刪除。
命令:
rm 文件名
輸出:
[root@localhost test1]# ll
總計 4
-rw-r--r-- 1 root root 56 10-26 14:31 log.log
root@localhost test1]# rm log.log
rm:是否刪除 一般文件 “l(fā)og.log”? y
root@localhost test1]# ll
總計 0[root@localhost test1]#
說明:
輸入rm log.log命令后,系統(tǒng)會詢問是否刪除,輸入y后就會刪除文件,不想刪除則數(shù)據(jù)n。
實例二:強行刪除file,系統(tǒng)不再提示。
命令:
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 *.log
rm:是否刪除 一般文件 “l(fā)og1.log”? y
rm:是否刪除 一般文件 “l(fā)og2.log”? y
[root@localhost test1]# ll
總計 0[root@localhost test1]#
實例四:將 test1子目錄及子目錄中所有檔案刪除
命令:
rm -r test1
輸出:
復(fù)制代碼代碼如下:[root@localhost test]# ll
總計 24drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 2 root root 4096 10-26 14:51 test1
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# rm -r test1
rm:是否進入目錄 “test1”? y
rm:是否刪除 一般文件 “test1/log3.log”? y
rm:是否刪除 目錄 “test1”? y
[root@localhost test]# ll
總計 20drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxr-xr-x 3 root root 4096 10-25 17:44 test2
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]#
實例五:rm -rf test2命令會將 test2 子目錄及子目錄中所有檔案刪除,并且不用一一確認
命令:
rm -rf test2
輸出:
復(fù)制代碼代碼如下:[root@localhost test]# rm -rf test2
[root@localhost test]# ll
總計 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]#
實例六:刪除以 -f 開頭的文件
命令:
rm -- -f
輸出:
復(fù)制代碼代碼如下:[root@localhost test]# touch -- -f
[root@localhost test]# ls -- -f
-f[root@localhost test]# rm -- -f
rm:是否刪除 一般空文件 “-f”? y
[root@localhost test]# ls -- -f
ls: -f: 沒有那個文件或目錄
[root@localhost test]#
也可以使用下面的操作步驟:
[root@localhost test]# touch ./-f
[root@localhost test]# ls ./-f
./-f[root@localhost test]# rm ./-f
rm:是否刪除 一般空文件 “./-f”? y
[root@localhost test]#
實例七:自定義回收站功能
命令:
myrm(){ D=/tmp/$(date +%Y%m%d%H%M%S); mkdir -p $D; mv "$@" $D && echo "moved to $D ok"; }
輸出:
復(fù)制代碼代碼如下:[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.log
drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-xr-x 3 root root 4096 10-25 17:56 test5
[root@localhost test]# rm [123].log
moved to /tmp/20121026150901 ok
[root@localhost test]# ll
總計 16drwxr-xr-x 7 root root 4096 10-25 18:07 scf
drwxrwxrwx 2 root root 4096 10-25 17:46 test3
drwxr-xr-x 2 root root 4096 10-25 17:56 test4
drwxr-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]#
說明:
上面的操作過程模擬了回收站的效果,即刪除文件的時候只是把文件放到一個臨時目錄中,這樣在需要的時候還可以恢復(fù)過來。
Linux刪除文件的命令?
1、刪除文件命令 rm -f 文件名 將會強行刪除文件,且無提示 需要注意:使用rm -rf要格外注意,linux中沒有回收站,慎重刪除 2、刪除文件夾以及文件夾中的所有文件命令:rm -rf 目錄名字 其中:-r:向下遞歸刪除 -f:直接強行刪除,且沒有任何提示 ...
linux shell 問題,清除文件中的空格
先建腳本:clearspace.sh 給腳本添加執(zhí)行權(quán)限 chmod +x clearspace.sh 編輯腳本內(nèi)容:touch tmpFile # 建臨時文件 sed 's\/\\ \/\/g' $1 >temFile #sed s命令 將空格替換 重定向到臨時文件 cat temFile >$1 #覆蓋原文件 rm -f temFile #刪除臨時文件 echo 'OK,DONE!'執(zhí)行腳本 .\/...
相關(guān)評說:
新密市懸架: ______ rm命令用于刪除文件或目錄,格式為:“rm [選項] 文件”.(備注:文件名可為多個) 在Linux系統(tǒng)中刪除文件時會默認再向您詢問是否要執(zhí)行刪除操作,如果不想總看到這種反復(fù)的確認信息,您可以使用“-f”參數(shù)來直接強制刪除,...
新密市懸架: ______ 直接rm就可以了,不過要加兩個參數(shù)-rf 即:rm -rf 目錄名字 -r 就是向下遞歸,不管有多少級目錄,一并刪除 -f 就是直接強行刪除,不作任何提示的意思 刪除文件夾實例:rm -rf /var/log/httpd/access 將會刪除/var/log/httpd/access目錄以及其下所有文件、文件夾 需要提醒的是:使用這個rm -rf的時候一定要格外小心,linux沒有回收站的,一旦刪除不可恢復(fù). 當(dāng)然,rm還有更多的其他參數(shù)和用法,man rm就可以查看了
新密市懸架: ______ rm: 刪除文件rm(remove)實用工具來刪除文件.格式: rm *(文件名) 刪除此文件后 執(zhí)行命令ls和cat,若ls沒有列出該文件,cat顯示沒有此文件,這表明利用rm所刪除的文件已不存在.使用rm要小心謹慎
新密市懸架: ______ mkdir /home/h 在home下 創(chuàng)建h目錄. mkdir h 在當(dāng)前目錄下創(chuàng)建h目錄
新密市懸架: ______ Linux刪除文件夾命令--rm, rmdir rmdir只能刪除空文件夾,如果文件夾非空,可以使用rm -rf命令,即:rm -rf 目錄名字.-r 就是向下遞歸,不管有多少級目錄,一并刪除-f 就是直接強行刪除,不作任何提示的意思 Linux重命名文件夾命令--mv 語法...
新密市懸架: ______ 您好,很樂意為您解答! 1. 如果是刪除某一類文件,可以rm *xxx.xx; 2. 如果是刪除目錄及下面的所有文件 rm -r xxx 3. 除了某一類全部刪除沒有直接的命令,只能用awt語言,這個我也不懂,你搜搜看 希望我的回答可以幫到您!
新密市懸架: ______ 后面跟上文件名,或者用通配符:rm file1 file2 file3 rm file?rm file*
新密市懸架: ______ rm -rf ./* 遞歸刪除當(dāng)前目錄下所有文件 rm -rf ./*.php遞歸刪除當(dāng)前目錄下所有php文件 -r 遞歸
新密市懸架: ______ rm 文件名即可如果不太懂可以鍵入 rm --help
新密市懸架: ______ 請自行安裝SHH遠程軟件、并能鏈接上linux服務(wù)器 用SSH登錄linux服務(wù)器 我們創(chuàng)建一個文件夾 創(chuàng)建完成 在linux下rm是刪除命令 先看rm的幫助說明 然后我們用rm test來刪除文件夾 提示無法刪除目錄 也就是rm不帶參數(shù)的時候只能刪除文件;而不能刪除文件夾 這次我們帶上-r參數(shù)來刪除文件夾 這個時候會提示我們是否刪除 輸入y 回車即可 刪除完成