在 Linux 系統中尋找檔案是基本功,但不同工具適用場景差異極大。本文深度解析 find、locate、whereis、which、type 五大查找指令的語法、參數與實戰範例,助你精準選擇最高效的工具。

find 直接掃描磁碟目錄樹,支援極豐富的條件判斷與後續動作,適合精確、複雜的搜尋需求。
find [指定目錄] [指定條件] [指定動作]
-print (預設)、-ls、-exec command {} \;、-delete 等。find . -name 'my*':當前目錄下找檔名以 my 開頭的檔案find . -name 'my*' -ls:並顯示詳細屬性find . -type f -mmin -10:找 10 分鐘內修改過的一般檔案 (-type f 排除目錄)find / -name "*.rpm" -exec chmod 755 {} \;:找到所有 .rpm 檔並修改權限find / -xdev -name "*.rpm":搜尋時忽略跨檔案系統 (如光碟、USB)locate 不掃描磁碟,而是查詢預建索引資料庫 (/var/lib/locatedb),速度極快,但無法找到最新建立/變動的檔案。
updatedb 手動更新資料庫locate /etc/sh:找 /etc 下以 sh 開頭的檔案locate ~/m:找家目錄下以 m 開頭的檔案locate -i ~/m:忽略大小寫僅搜尋預設系統目錄,適合確認指令安裝位置與文件。
whereis -b:只找二進制檔whereis -m:只找手冊頁whereis -s:只找原始碼$ whereis grep
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz
只回傳第一個符合的路徑,常用於確認「到底執行的是哪個版本」或「指令是否存在」。
$ which grep
/usr/bin/grep
type 不是搜尋工具,而是識別指令屬於 Shell 內建 (build-in) 還是外部二進制檔。
type cd → cd is a shell builtintype grep → grep is /usr/bin/greptype -p grep:等同 which grep,只顯示外部指令路徑| 指令 | 速度 | 資料即時性 | 適用場景 |
|---|---|---|---|
| find | 慢 | 即時 | 複雜條件、需對結果執行動作、尋找最新檔案 |
| locate | 極快 | 延遲 (每日更新) | 快速模糊搜尋檔名、不需即時結果 |
| whereis | 快 | 即時 | 定位指令的二進制、man page、source code |
| which | 快 | 即時 | 確認 PATH 中指令的實際執行路徑 |
| type | 極快 | 即時 | 區分內建/外部指令、獲取外部指令路徑 |
掌握這五把「尋檔利器」,無論是排查系統問題、開發除錯還是日常運維,都能事半功倍。