一個簡單的linux命令——rm

rm命令用於刪除文件或者目錄。對於鏈接文件,只是刪除鏈接。

一、命令格式

rm [option] file…


二、命令參數

  -f, --force    忽略不存在的文件,從不給出提示。
    -i, --interactive 進行交互式刪除
    -r, -R, --recursive   指示rm將參數中列出的全部目錄和子目錄均遞歸地刪除。
    -v, --verbose    詳細顯示進行的步驟
        --help     顯示此幫助信息並退出
        --version  輸出版本信息並退出

三、命令實例

1、刪除文件,系統會先詢問是否刪除

rm file

輸出:
[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:是否刪除 一般文件 “log.log”? y
root@localhost test1]# ll
總計 0[root@localhost test1]#

2、強行刪除文件,不提示

命令:
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]#

3、*刪除任何.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:是否刪除 一般文件 “log1.log”? y
rm:是否刪除 一般文件 “log2.log”? y
[root@localhost test1]# ll
總計 0[root@localhost test1]#

4、將 test1子目錄及子目錄中所有檔案刪除

命令:
rm -r test1

輸出:
[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]#

5、rm -rf test2命令會將 test2 子目錄及子目錄中所有檔案刪除,並且不用一一確認

命令:
rm -rf  test2 

輸出:
[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]#

6、自定義回收站功能

命令:
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.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]#
發佈了32 篇原創文章 · 獲贊 28 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章