Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 五大查找命令 find/locate/whereis/which/type 徹底比較

編輯:Linux 系統常見問題解答

命令定位對比表

命令搜索源搜索對象速度實時性典型用法
find磁盤遍歷文件/目錄 (名稱、類型、時間、大小、權限...)慢實時find / -name "*.log" -mtime -7
locate數據庫 (/var/lib/mlocate.db)文件名 (支持通配符、正則)極快非實時 (每日 updatedb 更新)locate -i "*.conf"
whereis數據庫 + 固定路徑二進制/源碼/手冊頁 (僅程序名)快非實時whereis -b nginx
whichPATH 環境變量可執行命令 (返回首個匹配)極快實時 (讀取當前 PATH)which python3
typeShell 內建 + PATH命令類型 (內建/別名/函數/外部文件)極快實時type -a ls

詳細用法示例

find - 最強大、最靈活


# 基礎
find . -name 'my*'                    # 當前目錄遞歸查找 my 開頭文件
find . -name 'my*' -ls                # 顯示詳細屬性
find . -type f -mmin -10              # 10 分鐘內修改過的普通文件

# 進階組合
find /var/log -name '*.log' -size +100M -exec gzip {} \;  # 壓縮大日誌
find / -user zhangsan -perm /u=x -type f                  # 屬於 zhangsan 且可執行的文件
find . \( -name "*.c" -o -name "*.h" \) -print            # 多條件 OR

locate - 秒級定位文件名


locate /etc/sh                # /etc 目錄下 sh 開頭文件
locate -i ~/m                 # 忽略大小寫查找家目錄 m 開頭
updatedb                      # 手動更新數據庫 (需 root)

whereis - 專查程序相關文件


whereis nginx                 # 顯示二進制、源碼、man 頁路徑
whereis -b nginx              # 僅顯示二進制
whereis -m nginx              # 僅顯示 man 頁

which - 確認命令實際執行路徑


which grep                    # /usr/bin/grep
which -a python               # 列出 PATH 中所有 python (查看被覆蓋的版本)

type - 區分命令本質


type cd                       # cd is a shell builtin
type ls                       # ls is aliased to `ls --color=auto'
type -p grep                  # 等同 which grep,僅顯示外部命令路徑
type -a ls                    # 顯示所有同名定義 (別名、函數、文件)

選擇建議

  • 知曉大約路徑、需按屬性 (時間/大小/權限) 篩選、或需對結果執行操作 → find。
  • 只記得文件名大約模樣、全盤快速定位 → locate (先 updatedb)。
  • 確認系統命令安裝位置、或排查 PATH 順序問題 → which。
  • 判斷命令是 Shell 內建、別名、函數還是外部程序 → type。
  • 查找某程序的二進制、源碼、文檔位置 → whereis。
Copyright © Windows教程網 All Rights Reserved