首頁·終端機命令

find

檔案與目錄

用法

find [path] -name '[pattern]' -type f

遍歷目錄樹逐一比對條件;把樣式寫成 -name '*.log' 加引號,否則 shell 會先把它展開。

常用選項

選項含義
-nameMatch the file name, case sensitive; -iname ignores case
-typeRestrict to f (file), d (directory) or l (symlink)
-sizeFilter by size, as in -size +100M
-mtimeFilter by age in days, as in -mtime +7
-maxdepthLimit how deep to walk; GNU wants it before the tests
-deleteDelete every match, no confirmation
-execRun a command on each hit, ending with \; or +
-print0Separate results with NUL, to pair with xargs -0

範例

find . -name '*.log' -mtime +7 -delete

Deletes log files older than a week.

find / -type f -size +1G 2>/dev/null

Finds files over a gigabyte, hiding permission errors.

find . -name '*.jpg' -exec mv {} photos/ +

Moves every JPEG it finds.

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

怎麼看

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

常見問題

Q. find 是做什麼的?

遍歷目錄樹逐一比對條件;把樣式寫成 -name '*.log' 加引號,否則 shell 會先把它展開。

Q. 怎麼寫?

find [path] -name '[pattern]' -type f —— 方括號表示可以省略的部分。

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

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

相關命令

man 手冊: man find