Shell 編程中的常用工具


文件查找 find 命令

語法格式

 

 

find命令總結:

常用選項:

    -name    查找/etc目錄下以conf結尾的文件 find /etc -name "*.conf"
    -iname    查找當前目錄下文件名為aa的文件,不區分大小寫    find . -iname aa
    -user    查找文件屬主為hdfs的所有文件    find . -user hdfs
    -group    查找文件屬主為yarn的所有文件    find . -group yarn
    -type    查找文件屬組為yarn的所有文件    find . -group yarn
        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的文件
        n    大小等於n的文件(用的少)

 

查找/etc/目錄下小於10000字節的文件

find /etc -size -10000c

 

查找/etc目錄下大於1M的文件

find /etc/ -size +1M

-mtime

  • -n n天以內修改的文件
  • +n n天以外修改的文件
  • n 正好等於n天修改的文件

 

查找/etc目錄下5天以內修改且以conf結尾的文件

find /etc -mtime -5 -name '*.conf'

 

查找/etc目錄下10天之前修改且屬主為root的文件

find /etc -mtime +10 -user root

  

-mmin -n

  • n分鍾以內修改的文件
  • +n n分鍾以外修改的文件

查找/etc目錄下30分鍾之前修改的文件

find /etc -mmin +30

 

查找/etc目錄下30分鍾以內修改的目錄

find /etc -mmin -30 -type d

  

-mindepth n 表示從n級子目錄開始搜索

在/etc下的3級子目錄開始搜索

find /etc -mindepth 3 -name '*.conf'

  

-maxdepth n 表示最多搜索到n級子目錄

在/etc下搜索符號條件的文件,但最多搜索到2級子目錄

find /etc -type f -name '*.conf' -size +10k -maxdepth 2

  

需要了解的選項:

-nouser 查找沒有屬主的文件

find . -type f -nouser

 

-nogroup 查找沒有屬組的文件

find . -type f -nogroup

 

-perm 根據文件權限查找

find . -perm 664

 

-prune 通常和-path一起使用,用於將特定目錄排除在搜索條件之外

查找當前目錄下所有普通文件,但排除test目錄

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

  

查找當前目錄下所有普通文件,但排除etc和opt目錄

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

  

查找當前目錄下所有普通文件,但排除etc和opt目錄,但屬主為hdfs

find . -path /etc -prune -o -path /opt -prune -o -type f -a -user hdfs

  

查找當前目錄下所有普通文件,但排除etc和opt目錄,但屬主為hdfs,切文件大小必須大於500字節

find . -path ./etc -prune -o -path ./opt -prune -o -type f -a user hdfs -a -size +500c

  

-newer file1  查找比file1 新的文件

find /etc -newer a

  

操作:

  • -print 打印輸出
  • -exec 對搜索到的文件執行特定的操作,格式為 -exec 'command' {} \;
  • -ok 和exec功能一樣,只是每次操作都會給用戶提示

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

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

  

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

find /var/log -name "*.log" -mtime +7 -exec rm -rf {} \;

  

例子3:搜索條件和例子1一樣,只是不刪除,而是將其拷貝到/root/conf目錄下

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

  

邏輯運算符:

  • -a 與
  • -o 或
  • -not|! 非

查找當前目錄下,屬主不是hdfs的所有文件

find . -not -user hdfs | find . ! -user hdfs

 

查找當前目錄下,屬主屬於hdfs,且大小大於300字節的文件

find . -type f -a -user hdfs -a -size +300c

  

查找當前目錄下的屬主為hdfs或者以xml結尾的普通文件

find . -type f -a \( -user hdfs -o -name '*.xml' \)

  

示例:

查找以.conf結尾的文件

find /etc -name '*.conf'

  

-name 區分大小寫,iname忽略大小寫

find ./ -iname 'aa'

查找文件

find ./ -type f

  

查找/etc/目錄下大於1M的文件

find /etc -size +1M

 

查找3天內修改的文件

find /etc/ -mtime -3

 

查找5天內的.conf文件

find /etc -mtime -5 -name "*.conf"

 

查找30分鍾內被修改的文件

find /etc -mmin -30

 

查找2級子目錄查找文件

find . -mindepth 2 -type f

 

最深查找1級子目錄的文件

find . -maxdepth 1 -type f

 

查找644權限的文件

find . -perm 644

 

排除 test1/nginx 目錄后的文件

find . -path ./test1/nginx -prune -o -type f

 

查找排除 test_1 和 test1 以后的文件

find . -path ./test_1 -prune -o -path ./test1 -prune -o -type f

  

查找當前目錄下比123 新的文件

find ./ -newer 123

 

將etc目錄拷貝到當前目錄,查找etc目錄中的.conf文件並刪除

cp -r /etc ./
find ./etc -name '*.conf' -exec rm -f {} \;

find ./etc -name '*.conf'

  

將etc目錄下大於1m的文件拷貝到test_5目錄下

find ./etc/ -size +1M

  

-ok 提示用戶是否執行操作

find ./ -type f -ok rm -f {} \;

  

 

find、locate、whereis和which總結及適用場景分析

 

locate 命令 

update db 命令 

 

find是精確查找

find /etc -name 'my.cnf'

 

locate部分匹配

locate my.cnf

 

即時創建的文件用locate是查找不到的,因為系統有計划任務定時更新mlocate.db文件,如果不包含是查找不到文件的

touch abc.txt
touch def.txt

# 查詢剛剛創建的發現查找不到
locate abc.txt
locate def.txt

# 更新數據庫就可以查找到文件了
ll -h /var/lib/mlocate/mlocate.db 
updatedb
locate abc.txt
locate def.txt

  

 

whereis命令

 

所有的文件都列出來

whereis mysql

 

只查找二進制文件

whereis -b mysql

 

 

只查找man文檔

whereis -m mysql

  

which命令

 

which mysql

  

  

各命令使用場景推薦

  • find 查找某一類文件,比如文件名部分一致 功能強大,速度慢
  • locate 只能查找單個文件 功能單一,速度快
  • whereis 查找程序的可執行文件、幫助文檔等 不常用
  • which 只查找程序的可執行文件 常用於查找程序的絕對路徑

 

 

 

  

 


免責聲明!

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



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