首頁·終端機命令

rm

檔案與目錄

用法

rm -rf [path]

徹底刪除檔案;沒有回收筒,rm -rf 裡多一個空格就會毀掉不該刪的東西。

常用選項

選項含義
-rDescend into directories and delete what is inside
-fNever prompt, and say nothing about files that are missing
-iAsk before every single removal
-dRemove an empty directory without -r
-vPrint each name as it goes
-IGNU: ask once before removing more than three files
-PmacOS: overwrite the file before unlinking it

範例

rm -rf build

Deletes the build directory and everything under it.

rm -i *.log

Asks about each log file first.

rm -- -weird

Deletes a file whose name starts with a dash.

搬移、刪除和尋找。有些命令不會再問一次,所以順手加 -i 是在救自己。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. rm 是做什麼的?

徹底刪除檔案;沒有回收筒,rm -rf 裡多一個空格就會毀掉不該刪的東西。

Q. 怎麼寫?

rm -rf [path] —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man rm。這條命令屬於檔案與目錄。

相關命令

man 手冊: man rm