Linux find命令


一、find命令語法格式:

  find [路徑] [選項] [操作]

 選項參數對照表:

  

   

三、常用選項

  -name  查找 /etc 目錄下以 conf 結尾的文件,文件名區分大小寫,例如:find /etc -name '*.conf'

  -iname      查找當前目錄下所有文件名為 aa 的文件,文件名不區分大小寫,例如:find . -name aa

    

  -user  查找文件所屬用戶為 yangyang 的所有文件,例如:find . -user yangyang

  -group    查找文件所屬組為 yangyang 的所有文件,例如:find . -group yangyang

  -type  根據類型查找:如下

      f   文件        find . -type f

      d  目錄        find . -type d

      c  字符設備文件    find . -type c

      b  塊設備文件     find . -type b

      l   鏈接文件      find . -type l

      p  管道文件      find . -type p

 

  -size    根據文件大小查詢

      -n  小於 大小為 n 的文件

      +n  大於 大小為 n 的文件

      舉例1:查找 /ect 目錄下,小於 10000 字節的文件。 find /etc -size +10000c

      舉例2:查找 /etc 目錄下,大於 1M 的文件。find /etc -size -1M

 

   -mtime  

      -n  n 天以內修改的文件。

      +n  n 天以外修改的文件。

      n    正好 n天 修改的文件

      舉例1: 查詢 /etc 目錄下,5天以內修改 且以 conf 結尾的文件。 find /etc -mtime -5 -name '*.conf'

        舉例2: 查詢 /etc 目錄下,10天之前修改,且屬於 yangyang 的文件。  find /etc -mtime +10 -user yangyang

 

  -mmin  

      -n  n 分鍾以內修改過的文件

        +n  n 分鍾之前修改過的文件

      舉例1: 查詢 /etc 目錄下 30分鍾 之前修改過的文件。   find /etc -mmin +30

        舉例1: 查詢 /etc 目錄下 30分鍾 之前修改過的目錄。   find /etc -mmin -30 -type d

 

  -mindepth n  從第 n 級目錄開始搜索

      舉例:從 /etc 的第三級子目錄開始搜索。  find /etc -mindepth 3

 

  -maxdepth n  表示至多搜索到第 n-1 級子目錄。

      舉例1: 在 /etc 中搜索符合條件的文件,但最多搜索到 2級 子目錄。  find /etc -maxdepth 3 -name '*.conf'

      舉例2: find /etc -type f -name '*.conf' -size +10k -maxdepthc 2

 

四、不常用選項

  -nouser     查詢沒有所屬用戶的文件

      舉例:find /etc -type f -nouser

  -nogroup 查詢沒有所屬組的文件

      舉例:find /etc -type f -nogroup

  -perm  根據權限查詢

      舉例:find /etc -perm 664

 

  -prune  通常和 -path 一起使用,用於將特定目錄排除在搜索條件之外。過濾條件寫在其他條件前面。

      舉例1:查找當前目錄下的所有普通文件,但排除 test目錄。

        find . -path ./test -prune -o -type f

      舉例2:  查找當前目錄下所有普通文件,但排除 test目錄 和 opt目錄。

        find . -path ./test -prune -o -path ./opt -prune -o -type f  

      舉例3:  查找當前目錄下所有普通文件,但排除 test目錄 和 opt目錄,但屬主為 yangyang

        find . -path ./test -prune -o -path ./opt -prune -o -type f -a -user yangyang

      舉例4:  查找當前目錄下所有普通文件,但排除 test目錄 和 opt目錄,但屬主為 yangyang,且文件大小必須大於 500字節

        find . -path ./test -prune -o -path ./opt -prune -o -tyep f -a -user yangyang -a -size +500

 

  -newer file1 

      舉例:查找當前目錄下比 a 文件要新的文件。  find . -newer a  

五、操作查找到的文件 

  -print  打印輸出。 默認的選項,即打印出找到的結果。

  -exec  對搜索到的文件執行特定的操作,固定的格式為:-exec 'commond' {} \;   注意:{} 表示查詢的結果。

      舉例1: 搜索 /etc 目錄下的文件(非目錄),文件以 conf 結尾,且大於 10k,然后將其刪除。

        find /etc -type f -name '*.conf' -size +10k -exec rm -f {} \;

      舉例2: 將 /data/log/ 目錄下以 .log 結尾的文件,且更改時間在 7 天以上的刪除。

        find /data/log -name '*.log' -mtime +7 -exec rm -f \;

      舉例3: 搜索條件同 例1 一樣,但是不刪除,只是將其復制到 /root/conf 目錄下

        find /etc -type f -name '*.conf' -size +10k -exec cp {} /root/conf/ \;

  -ok  -exec 的功能一樣,只是每次操作都會給用戶提示。  

六、邏輯運算符 

  -a    與  (默認情況查詢條件之間都是 與 的關系)

  -o    或

  -not | !  非 

  

 

為了三五計划,加油!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM