Skip to content

Instantly share code, notes, and snippets.

@lazyjerry
Created October 4, 2017 13:58
Show Gist options
  • Save lazyjerry/43b6af787be4992a5d362c0a5ddbc2e3 to your computer and use it in GitHub Desktop.
Save lazyjerry/43b6af787be4992a5d362c0a5ddbc2e3 to your computer and use it in GitHub Desktop.
find -name breakfast.txt 在工作目錄下,含裡面所有目錄,尋找 breakfast.txt
find /home/whale -name "*.txt" 在/home/whale目錄下找所有副檔名為txt的檔案名稱
find -type f -name "v*" 在工作目錄下,找開頭是v的一般檔案(不是目錄,不是link...)
find -type f -size +100k 在工作目錄下,找大於100k的檔案.
find -type f -ctime +2 在工作目錄下,找48小時前(2*48)建立(create)的檔案
find -type f -ctime 2 在工作目錄下,找介於前72小時到前48小時之間建立的檔案
find -type f -amin -3 在工作目錄下,找三分鐘內有存取(access)的檔案
find -type f -size 2000k -printf "%f %AD %k\n" , 找大於2000k的檔案,並且用printf指定的format來結果, %f 是不含path的檔名, %AD是 mm/dd/yy, %k 是檔案大小(單位: 1Kb); 更多參數請 man find
find -type f -exec ls -al {}\; 在工作目錄,找所有一般檔案,並且對每個檔名執行 ls -al 命令.
find -type f -name "*.log" -exec rm -f {} \; 在工作目錄,找副檔名為log的檔案,並且用rm -f 刪除
find -type f -name "*.log" -exec grep Error {} \;在工作目錄,找副檔名為log的檔案,並且用grep尋找這些檔案中含有 Error字串的行
註:
find -type f -name "*.log" -exec grep Error {} \; 也可以用
find -type f -name "*.log" xargs grep Error
二者主要的差異是後者執行效率比較好,如果總共有 100 個 .log檔,那前者(find)會執100次 grep, 可是後者xargs的做法會一次把所有的檔名讀進來給grep處理, 所以grep 只會執行一次.
| wc -l 查看檔案數量
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment