基本語法
find [搜尋路徑] [選項] [動作]
常用選項:
-name "pattern" 按名稱匹配 (支援通配符)
-type f/d/l 文件/目錄/符號鏈接
-size +100M 大於 100MB
-mtime -7 7 天內修改過
-user username 按擁有者
-perm 644 按權限
-exec cmd {} \; 對結果執行命令
常用範例
| 需求 | 命令 |
| 全盤查找名為 sphinx.conf 的文件 | find / -name sphinx.conf |
| 查找 /etc 下所有 .conf 文件 | find /etc -name "*.conf" -type f |
| 查找超過 100MB 的文件 | find / -type f -size +100M |
| 查找 7 天前修改的日誌文件 | find /var/log -name "*.log" -mtime +7 |
| 查找並刪除臨時文件 | find /tmp -name "*.tmp" -delete |
| 查找並批量修改權限 | find /web -type f -exec chmod 644 {} \; |
進階技巧
- 避開特定目錄:
find / -path "/proc" -prune -o -name "target" -print
- 組合條件:
find / -type f \( -name "*.log" -o -name "*.txt" \) -size +10M
- 配合 xargs 高效處理:
find . -name "*.jpg" -print0 | xargs -0 tar czf images.tar.gz
速查:locate vs find
locate:查詢預建資料庫 (updatedb),極快但非即時
find:即時遍歷磁碟,支援豐富條件與動作