實(shí)例講解linux強(qiáng)大的find命令

find命令思維導(dǎo)圖

Find命令是linux中最常用且重要的命令之一,用于檢索文件所在的位置,可以根據(jù)多種參數(shù)組合進(jìn)行檢索:文件名稱,文件權(quán)限,文件屬組,文件類型,文件大小等。

雖然man find手冊有關(guān)于find的詳細(xì)說明,可缺乏實(shí)例的說明文檔顯得干巴巴,對初學(xué)者很不友好。導(dǎo)致初學(xué)者對于find產(chǎn)生這樣的印象:“我知道find很強(qiáng)大,但不知道用在什么場景,該怎么用”。

再強(qiáng)大的工具,只有會用,用得好,才能體現(xiàn)出其價值。

基于此,本文將用實(shí)例講解find命令常用場景:

基本使用

-name 指定文件名

$ find /etc -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd

find會對指定路徑進(jìn)行遞歸查找

-iname 忽略大小寫

$ find . -iname test.txt
./TesT.txt
./Test.txt
./test.txt

-type d 查找目錄

$ find . -type d -name dir1
./dir1

-type f 查找文件

$ find . -type f -name test.php
./test.php

查找某一類文件

$ find . -type f -name "*.php"
./test.php
./test1.php
./test2.php

* 表示通配符

根據(jù)權(quán)限查找

查找權(quán)限為777的文件

$ find . -type f -perm 777 -print

-print 將結(jié)果打印

查找權(quán)限不為777的文件

$ find . -type f ! -perm 777

! 反選

查找可執(zhí)行文件

即查找所有用戶都擁有x權(quán)限的文件

$ find . -type f -perm /a=x

找到777權(quán)限的文件并將其改為644

$ ll
-rwxrwxrwx 1 root root 0 9月  17 22:01 test

$ find -type f -perm 777 -print -exec chmod 644 {} \;
./test

$ ll                                                 
-rw-r--r-- 1 root root 0 9月  17 22:01 test

查找并刪除單一文件

$ find . -type f -name "test" -exec rm -f {} \;

查找并刪除多個文件

$ find . -type f -name "*.txt" -exec rm -f {} \;

查找所有空文件

$ find /tmp -type f -empty

查找所有空目錄

$ find /tmp -type d -empty

查找所有隱藏文件

$ find . -type f -name ".*"

根據(jù)屬主/屬組查找文件

$ find /etc -user senlong -name passwd

$ find /etc -user root -name passwd   
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd

$ find /etc -group root -name passwd
/etc/cron.daily/passwd
/etc/pam.d/passwd
/etc/passwd

根據(jù)文件時間查找

50天前修改過的文件

$ find . -mtime 50

大于50天小于100天前修改過的文件

$ find . -mtime +50 -mtime -100

根據(jù)文件大小查找

查找大小為50M的文件

$ find / -size 50M

查看大小為50M至100M的文件

$ find / -size +50M -size -100M

查找大于100M的log文件并刪除

$ find / -type f -name "*.log" -size +100M -exec rm {} \;

思維導(dǎo)圖源文件下載

參考鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容