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

Linux find 指令完全指南:語法、邏輯運算子與 20 種常用查詢選項詳解

編輯:系統常見問題解答

find 是 Linux 系統中最強大的檔案搜尋工具,支援遞迴搜尋、複合條件與動作執行。本文詳細拆解語法結構、三大邏輯運算子(and/or/not)與 20 種核心查詢選項,並提供實戰範例。

基本語法

find [起始目錄] [查詢條件] [動作]
# 等價於
find PATH OPTION [-exec COMMAND {} \;]

起始目錄:搜尋的根路徑,find 將遞迴遍歷其下所有子目錄。

邏輯運算子

運算子符號說明範例
AND-a (預設)所有條件皆滿足find -name 'tmp' -a -xtype c -a -user 'inin'
OR-o任一條件滿足即可find -name 'tmp' -o -name 'mina*'
NOT!排除滿足條件的檔案find ! -name 'tmp'

複雜條件可用 \( \) 分組,括號前需加反斜線轉義:find \( -name 'tmp' -a -xtype c -a -user 'inin' \)

20 種核心查詢選項

選項功能範例
-name 'pattern'檔名匹配(支援 * ? [])find -name '*.log'
-lname 'pattern'符號連結檔名匹配
-gid n群組 ID 為 n
-uid n用戶 ID 為 n
-group 'name'群組名稱
-user 'name'用戶名稱
-empty大小為 0 的檔案或目錄
-path 'pattern'路徑匹配
-perm mode權限匹配(如 711, 644)
-size n[bckw]檔案大小(預設 b=512 bytes)find -size +100M
-type x檔案類型:f(檔案) d(目錄) l(連結) c(字元裝置) b(區塊裝置) s(socket) p(pipe)
-mtime n修改時間 n 天前
-atime n存取時間 n 天前
-ctime n狀態改變時間 n 天前
-newer file比指定檔案更新
-exec cmd {} \;對結果執行命令find -name '*.tmp' -exec rm {} \;
-ok cmd {} \;執行前詢問確認
-print輸出路徑(預設動作)
-printf 'format'格式化輸出
-maxdepth n最大遞迴深度
Copyright © Windows教程網 All Rights Reserved