LINUX下find命令小記

find 命令

find 路徑 查找條件 查找動作

(1)根據文件名或正則表達式匹配搜索

eg. find /home -name "*.txt" -print

iname表示忽略大小寫

a.匹配2個條件

find /home \( -name "*.txt" -a -name "user.*" \) -print

b.匹配2個條件中的一個

find /home \( -name "*.txt" -o -name "user.*" \) -print

c.否定參數 !

eg. find . ! -name "*.txt" -print 匹配不以.txt結尾的文件

(2)根據文件類型搜索

eg.find . -type d -print

文件類型類型參數

普通文件f

符號連接l

目錄d

字符設備c

塊設備b

套接字s

Fifop

(3)根據文件時間進行搜索

三種時間戳

訪問時間(-atime): 用戶最近一次訪問文件的時間

修改時間(-mtime): 文件內筒最後一次修改的時間

變化時間(-ctime): 文件元數據(metadata, 例如權限或所有權)最後一次改變的時間。

eg. find . -type f -atime -7 -print  最進7天內被訪問的所有文件

find . -type f -atime 7 -print 恰好7天前被訪問過的所有文件

find . -type f -atime +7 -print 訪問時間超過7天

表示分鐘的參數:

-amin: 訪問時間

-mmin:修改時間

-cmin:變化時間

-newer選項

find . -type f -newer file.txt -print : 打印比file.txt修改時間更長的所有文件

(4)基於文件大小查找

find . -type f -size +2k :大於2K的文件

find . -type f -size 2k  :大小等於2K的文件

find . -type f -size -2k :大小小於2K的文件愛你

常用單位:b(塊), c(字節), w(字), k(KB), M(兆), G

(5)刪除匹配的文件

-delete 可以用來刪除匹配到的文件

find . -type f -name "*.txt" -delete

(5)基於文件權限和所有權查找

find . -type f -perm 644 -print :查找644權限文件

find . -type f -user(用戶名或ID) mgx -print

(6)結合-exec 執行命令或動作

-exec選項

eg. find . -type f -user root -exec chown mgx {} \;

(7)結合xargs

eg. find . -type f -name "*.txt" | xargs rm -f

無法預測分割find命令輸出結果的是'\n'還是' ';有時很多文件名包含空格符,而xargs則可能會誤以為它們是定界符,eg.hell ta.txt

可能會被誤認為是hell 和ta.txt.

因此,常使用-print0結合使用

eg. find . -type f -name "*.txt" -print0 | xargs rm -f


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章