轉載:https://blog.csdn.net/qq_39539367/article/details/81742645php
man文檔中給出的find命令的通常形式爲:正則表達式
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]shell
其實[-H] [-L] [-P] [-D debugopts] [-Olevel]這幾個選項並不經常使用(至少在個人平常工做中,沒有用到過),上面的find命令的經常使用形式能夠簡化爲:express
find [path...] [expression] 即 :find 【路徑名】 【選項】 【查找的文件名或者目錄名】安全
- path:find命令所查找的目錄路徑。例如用.來表示當前目錄,用/來表示系統根目錄post
- expression:expression能夠分爲——「-options [-print -exec -ok ...]」spa
- -options,指定find命令的經常使用選項,下節詳細介紹.net
- -print,find命令將匹配的文件輸出到標準輸出debug
- -exec,find命令對匹配的文件執行該參數所給出的shell命令。相應命令的形式爲'command' { } \;,注意{ }和\;之間的空格3d
- find ./ -size 0 -exec rm {} \; 刪除文件大小爲零的文件 (還能夠以這樣作:rm -i `find ./ -size 0` 或 find ./ -size 0 | xargs rm -f &)
- 爲了用ls -l命令列出所匹配到的文件,能夠把ls -l命令放在find命令的-exec選項中:find . -type f -exec ls -l { } \;
- 在/logs目錄中查找更改時間在5日之前的文件並刪除它們:find /logs -type f -mtime +5 -exec rm { } \;
- -ok,和-exec的做用相同,只不過以一種更爲安全的模式來執行該參數所給出的shell命令,在執行每個命令以前,都會給出提示,讓用戶來肯定是否執行。
- find . -name "*.conf" -mtime +5 -ok rm { } \; 在當前目錄中查找全部文件名以.LOG結尾、更改時間在5日以上的文件,並刪除它們,只不過在刪除以前先給出提示
也有人這樣總結find命令的結構:
find start_directory test
options
criteria_to_match
action_to_perform_on_results
記住:對於路徑名來講,若想從某一目錄開始查找,則必定須要須要寫成絕對路徑的形式,但若以當前目錄開始查找,能夠直接用「 . 」代替路徑名。
eg:
對於當前目錄沒有這個文件或者同名的時候,直接執行find將不會返回任何值
find .:列出當前目錄下的全部文件和目錄
find /home/admin/text -name kuo :
顯示text目錄下全部同名字的文件或者文件名
find /home/admin/text -iname kuo:
顯示不區分大小寫的全部文件或文件名
find . -name "*.txt" -o -name "*.pdf" :
查找以 . txt和 . pdf爲結尾的文件
匹配文件路徑或者文件
: find /usr/ -path "*local*"
基於正則表達式匹配文件路徑 find . -regex ".*\.txt∥\.pdf\.txt‖\.pdf$"
同上,但忽略大小寫
find . -iregex ".*\.txt∥\.pdf\.txt‖\.pdf$"
否認參數:
找出/home下不是以.txt結尾的文件:
find /home ! -name "*.txt"
根據文件類型進行搜索 :
類型參數列表: f 普通文件 l 符號鏈接 d 目錄 c 字符設備 b 塊設備 s 套接字 p Fifo
find . -type 【類型參數】
基於目錄深度搜索 :
向下最大深度限制爲3
find . -maxdepth 3 -type f
搜索出深度距離當前目錄至少2個子目錄的全部文件
find . -mindepth 2 -type f
根據文件時間戳進行搜索:
find . -type f 時間戳
UNIX/Linux文件系統每一個文件都有三種時間戳:
訪問時間 (-atime/天,-amin/分鐘):用戶最近一次訪問時間。
修改時間 (-mtime/天,-mmin/分鐘):文件最後一次修改時間。
變化時間 (-ctime/天,-cmin/分鐘):文件數據元(例如權限等)最後一次修改時間。
搜索最近2天內被訪問過的全部文件
find . -type f -atime -2
搜索剛好在七天前被訪問過的全部文件
find . -type f -atime 7
搜索超過七天內被訪問過的全部文件
find . -type f -atime +7
索訪問時間超過10分鐘的全部文件
find . -type f -amin +10
找出比file.log修改時間更長的全部文件
find . -type f -newer file.log
根據文件大小進行匹配 :
文件大小單元: b —— 塊(512字節) c —— 字節 w —— 字(2字節) k —— 千字節 M —— 兆字節 G —— 吉字節
find . -type f -size [文件大小單元]
搜索大於10KB的文件
find . -type f -size +10k
搜索小於10KB的文件
find . -type f -size -10k
搜索等於10KB的文件
find . -type f -size 10k
刪除匹配文件
刪除當前目錄下全部.txt文件
find . -type f -name "*.txt" -delete
根據文件權限/全部權進行匹配
當前目錄下搜索出權限爲777的文件
find . -type f -perm 777
找出當前目錄下權限不是644的php文件
find . -type f -name "*.php" ! -perm 644
找出當前目錄用戶tom擁有的全部文件
find . -type f -user tom
找出當前目錄用戶組sunk擁有的全部文件
find . -type f -group sunk
藉助-exec選項與其餘命令結合使用
找出當前目錄下全部root的文件,並把全部權更改成用戶tom
find .-type f -user root -exec chown tom {} \;
上例中,{} 用於與-exec選項結合使用來匹配全部文件,而後會被替換爲相應的文件名。
找出本身家目錄下全部的.txt文件並刪除
find $HOME/. -name "*.txt" -ok rm {} \;
上例中,-ok和-exec行爲同樣,不過它會給出提示,是否執行相應的操做。
查找當前目錄下全部.txt文件並把他們拼接起來寫入到all.txt文件中
find . -type f -name "*.txt" -exec cat {} \;> all.txt
將30天前的.log文件移動到old目錄中 find . -type f -mtime +30 -name "*.log" -exec cp {} old \;
找出當前目錄下全部.txt文件並以「File:文件名」的形式打印出來
find . -type f -name "*.txt" -exec printf "File: %s\n" {} \;
由於單行命令中-exec參數中沒法使用多個命令,如下方法能夠實如今-exec以後接受多條命令
-exec ./text.sh {} \;
搜索但跳出指定的目錄
查找當前目錄或者子目錄下全部.txt文件,可是跳過子目錄sk
find . -path "./sk" -prune -o -name "*.txt" -print
find其餘技巧收集 要列出全部長度爲零的文件
find . -empty