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 是 Linux 最強大的檔案搜尋工具,支援多條件組合、動作執行與格式化輸出。本文系統整理常用語法與實戰範例。

一、基本語法

find [路徑] [條件] [動作]

二、依檔名查找

需求指令
完整檔名find / -name httpd.conf
部分檔名 (含 srm)find /etc -name '*srm*'
開頭為 srmfind /etc -name 'srm*'
限定目錄加速find /etc -name httpd.conf (比全盤搜尋快)

三、忽略權限錯誤輸出

find / -name access_log 2>/dev/null

將 stderr 導向 /dev/null 隱藏 Permission denied。

四、依檔案屬性/時間/大小查找

條件指令範例說明
-sizefind / -size +10000000c大於 10MB (c=bytes, k=KB, M=MB, G=GB)
-aminfind / -amin -1010 分鐘內被存取
-atimefind / -atime -248 小時內被存取
-mminfind / -mmin -55 分鐘內被修改
-mtimefind / -mtime -124 小時內被修改
-emptyfind / -empty空檔案或空目錄
-userfind / -user fred屬於使用者 fred
-groupfind / -group dev屬於群組 dev
-nouserfind / -nouser無對應使用者 (UID 不存在)
-nogroupfind / -nogroup無對應群組

五、邏輯組合 (AND/OR/NOT)

# AND:同時符合
find /tmp -size +10M -and -mtime -1

# OR:符合任一
find / -user fred -or -user george

# NOT:排除
find /tmp ! -user panda

六、動作參數

參數用途範例
-ls顯示詳細資訊 (同 ls -lids)find / -name "httpd.conf" -ls
-exec cmd {} \;對每個結果執行指令find /tmp -name "*.log" -exec rm -f {} \;
-ok cmd {} \;執行前逐一確認find /tmp -name "*.tmp" -ok rm {} \;
-printf "fmt"自訂格式輸出find / -name "*.c" -printf "%p %s\n"
-print0以 null 分隔輸出 (配合 xargs -0)find / -name "*.bak" -print0 | xargs -0 rm

七、深度控制

# 只搜尋前 2 層目錄
find . -maxdepth 2 -name fred

八、常用組合招

  • 刪除 30 天前的 .log:find /var/log -name "*.log" -mtime +30 -delete
  • 尋找大檔案佔用空間:find / -type f -size +100M -exec ls -lh {} \;
  • 批次修改權限:find /web -type f -exec chmod 644 {} \;
  • 尋找斷連結:find / -xtype l

熟練 find 即可掌控檔案系統的每一個角落。

Copyright © Windows教程網 All Rights Reserved