linux find文件查找用法

find語法如下:

find(選項)(參數)

根據文件名查找

1.列出當前目錄以及子目錄下的所有文件 find .

root@ubuntu:/home/test# find .
.
./11.png
./33.test
./22.jpg
./44.gif 

2.找到當前目錄下名字爲 11.png的文件 find . -name "11.png"

root@ubuntu:/home/test# find . -name "11.png"
./11.png

3.找到當前目錄下所有的 jpg文件 find . -name "*.jpg"

 

root@ubuntu:/home/test# find . -name "*.jpg"
./22.jpg

4.找到當前目錄下的 jpg文件和 png文件 find . -name "*.jpg" -o -name "*.png"

root@ubuntu:/home/test# find . -name "*.jpg" -o -name "*.png"
./11.png
./22.jpg

5.找出當前目錄下不是以 png結尾的文件 find . ! -name "*.png"

root@ubuntu:/home/test# find . ! -name "*.png"
.
./33.test
./22.jpg
./44.gif

 

根據正則表達式查找

備註:正則表示式比原先想的要複雜,支持好幾種類型。可以參考這裏

1.找到當前目錄下,文件名都是數字的 png文件。 find . -regex "\./*[0-9]+\.png"

root@ubuntu:/home/test# find . -regex "\./*[0-9]+\.png"
./11.png

 

2.根據路徑查找

找出當前目錄下,路徑中包含 wysiwyg的文件/路徑。 find . -path "*test*"

root@ubuntu:/home/test# find . -path "*test*"
./33.test
./tttestw

 

3.根據文件類型查找

通過 -type進行文件類型的過濾。

  • f 普通文件

  • l 符號連接

  • d 目錄

  • c 字符設備

  • b 塊設備

  • s 套接字

  • p Fifo

舉例,查找當前目錄下,路徑中包含 test的文件 find . -type f -path "*test*"

root@ubuntu:/home/test# find . -type f -path "*test*"
./33.test
./test.test

限制搜索深度

找出當前目錄下所有的 png,不包括子目錄。 find . -maxdepth 1 -name "*.png"

 

root@ubuntu:/home/test# find . -maxdepth 1 -name "*.png"
./11.png

root@ubuntu:/home/test# find . -maxdepth 2 -name "*.png" 
./11.png
./tttestw/11.png

相對應的,也是 mindepth選項。 find . -mindepth 1 -maxdepth 2 -name "*.png"

root@ubuntu:/home/test# find . -mindepth 1 -maxdepth 2 -name "*.png"
./11.png
./tttestw/11.png

 

根據文件大小

通過 -size來過濾文件尺寸。支持的文件大小單元如下

  • b —— 塊(512字節)

  • c —— 字節

  • w —— 字(2字節)

  • k —— 千字節

  • M —— 兆字節

  • G —— 吉字節

舉例來說,找出當前目錄下文件大小超過100M的文件 find . -type f -size +100M

root@ubuntu:/home/test# find . -type f -size +100M

根據訪問/修改/變化時間

支持下面的時間類型。

  • 訪問時間(-atime/天,-amin/分鐘):用戶最近一次訪問時間。

  • 修改時間(-mtime/天,-mmin/分鐘):文件最後一次修改時間。

  • 變化時間(-ctime/天,-cmin/分鐘):文件數據元(例如權限等)最後一次修改時間。

舉例,找出1天內被修改過的文件 find . -type f -mtime -1

root@ubuntu:/home/test# find . -type f -mtime -1

找出最近1周內被訪問過的文件 find . -type f -atime -7

root@ubuntu:/home/test# find . -type f -atime -7

 

將日誌目錄裏超過一個禮拜的日誌文件,移動到 /home/test/tttestw裏。find . -type f -mtime +7 -name "*.log" -exec mv {} /home/test/tttestw \;

root@ubuntu:/home/test# find . -type f -mtime +7 -name "*.log" -exec mv {} /home/test/tttestw \;

注意:{} 用於與-exec選項結合使用來匹配所有文件,然後會被替換爲相應的文件名。

另外, \;用來表示命令結束,如果沒有加,則會有如下提示 find: -exec: no terminating ";" or "+"

 

根據權限

通過 -perm來實現。舉例,找出當前目錄下權限爲 777的文件 find . -type f -perm 777

root@ubuntu:/home/test# find . -type f -perm 777

找出當前目錄下權限不是644的png文件 find . -type f -name "*.png" ! -perm 644

root@ubuntu:/home/test# find . -type f -name "*.png" ! -perm 644

根據文件擁有者

找出文件擁有者爲 root的文件 find . -type f -user root

 

root@ubuntu:/home/test# find . -type f -user root

 

找出文件所在羣組爲 root的文件

find . -type f -group root

root@ubuntu:/home/test# find . -type f -group root

 

找到文件後執行命令

通過 -ok、和 -exec來實現。區別在於, -ok在執行命令前,會進行二次確認, -exec不會。

看下實際例子。刪除當前目錄下所有的 js文件。用 -ok的效果如下,刪除前有二次確認。 find . -type f -name "*.js" -ok rm {} \;

root@ubuntu:/home/test# find . -type f -name "*.jpg" -ok rm {} \;
< rm ... ./tttestw/22.jpg > ? 

 

試下 -exec,直接就刪除了: find . -type f -name "*.jpg" -exec rm {} \;

root@ubuntu:/home/test# find . -type f -name "*.jpg" -exec rm {} \;  

 

找出空文件 find . -empty

例子如下

 

root@ubuntu:/home/test# touch {1..5}.txt
root@ubuntu:/home/test# echo "hello" > 1.txt
root@ubuntu:/home/test# find . -empty
./3.txt
./4.txt
./2.txt
./5.txt

 

發佈了47 篇原創文章 · 獲贊 62 · 訪問量 41萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章