今天要介紹和上一篇同樣是搜尋工具的 Linux 指令 — find。
在 Linux 系統中,find 是一個強大且靈活的指令,用來在檔案系統中搜尋檔案和目錄,並根據條件執行動作。無論是要找檔案名稱、修改時間、檔案大小,甚至是依權限或內容篩選,find 都能做到。尤其是對於系統管理者,更是會常常使用到這個工具。
find [搜尋路徑] [搜尋條件] [動作]
find . -name "file.txt" #在目前目錄找檔名為file.txt的檔案
find /home -name "file.txt" #在/home目錄下找檔名為file.txt的檔案
輸出會是從指定目錄開始列出的路徑
find /var/log -iname "file.txt" #不分大小寫找檔名
find /var/log -name "*.txt" #用萬用字元找尋
find / -type f # 搜尋檔案 (file)
find / -type d # 搜尋目錄 (directory)
find / -type l # 搜尋符號連結 (symbolic link)
find / -size +100M # 大於 100 MB
find / -size -10k # 小於 10 KB
k
→ KBM
→ MBG
→ GBfind / -mtime +30 # 30 天前修改過
find / -atime -1 # 最近 1 天內讀取過
find . -mtime +7 -mtime -14 #是在 7 天以上、14 天以下修改過
find / -perm 644 # 權限完全等於 644
find / -perm -222 # 至少有寫入權限的檔案
以上指令也可以搭配組合使用
find / -type f -size +1G #找大於 1GB 的檔案
find /var/www -type f -mtime -1 #找最近一天內修改的檔案
find / -type f ! -perm /u=r #找所有沒有讀取權限的檔案