find 的威力在於複合條件邏輯組合。本文整理十個實戰練習,涵蓋擁有者、權限、時間、大小、類型等多維度查詢,助您熟練掌握「精準找檔」技巧。
root、bin、hadoop、bash、rainman (UID 507)/var、/usr、/etc、/find /var \( -user root -a -group mail \) -ls
# 結果:/var/spool/mailfind /usr ! \( -user root -o -user bin -o -user hadoop \) -exec ls -l {} \;
# 結果:/usr/libexec/abrt-action-install-debuginfo-to-abrt-cache (屬 abrt)find /etc ! \( -user root -o -user hadoop \) -mtime -7 -exec ls -l {} \;
# 結果:/etc/find_lab/find.txt (屬 bash)find / -atime -7 \( -nouser -o -nogroup \) -ls
# 發現 rainman 相關檔案、/lab/readme.txt 等(含 /proc 錯誤可忽略)find /etc -type f -size +20k -exec ls -l {} \; | head -n 10
# 列出 httpd.conf、dnsmasq.conf、gimp gimprc 等大型設定檔find /home -type f -size +50M -atime +30 -lsfind / -type f \( -perm -4000 -o -perm -2000 \) -exec ls -l {} \;find /var/log -type f -exec ls -lt {} + | head -n 10find /tmp -type f -mtime +7 ! -user root -deletefind / -xtype l -ls
# 或 find / -type l ! -exec test -e {} \; -print() 分組、-a AND、-o OR、! NOT(需轉義 \( \))-exec cmd {} \; 單檔執行;-exec cmd {} + 批次執行(效率較高)-ls 等同 -exec ls -dils {} \;,快速看詳細資訊-mtime n(n*24h 前修改)、-mtime -n(n*24h 內)、-mtime +n(超過 n*24h)熟練這十類組合,應付 90% 的檔案稽核、清理、遷移需求綽綽有餘。