Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux find 命令查找文件與目錄實用指南

編輯:Linux 教學

基本語法

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:即時遍歷磁碟,支援豐富條件與動作
Copyright © Windows教程網 All Rights Reserved