歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux教程

Linux基礎知識之find命令詳解

在運維人員操作系統時,要接觸大量的文件,為了避免忘記文件存放位置的尴尬,就需要我們有一種文件查找工具的幫忙,下面是兩個文件查找工具的詳解,locate以及find,分別分享給大家。

第一款工具: Locate

locate - find files by name

locate的工作依賴於事先構建好的索引庫;查找文件時,直接搜索索引庫裡記載的文件的位置;

索引庫的構建:

 系統自動實現(周期性任務);

 手動更新數據庫(updatedb),但是索引構建的過程需要遍歷整個文件系統,極其耗費系統資源;

    updatedb - update a database for mlocate;

工作特性:

  查詢速度快,但不一定精確,無法匹配到數據庫創建後的創建文件;

  非實時查找,不能實時反饋當前文件系統上的文件狀態 ;

使用方法:

  locate [OPTIONS] FILE..

   選項:

   -c:統計查找結果的數量

    -b:只匹配路徑中的基名

   -r:基於基本正則表達式寫匹配模式

1 2 3 4 5 6 7 8 9 10 11 12 [root@linuxidc ~]# locate -c  "passwd" 165 [root@linuxidc ~]# locate -c  "inittab" 4    [root@linuxidc ~]# locate -b  "inittab" /etc/inittab /usr/local/share/man/zh_CN/man5/inittab.5 /usr/share/augeas/lenses/dist/inittab.aug /usr/share/vim/vim74/syntax/inittab.vim    [root@linuxidc ~]# locate -r  "/passwd

第二款工具:Find

find: find - search for files in a directory hierarchy

工作方式:通過遍歷指定起始路徑下文件系統層級結構完成文件查找;

工作特性:

  查找速度慢;

  精確查找;

  實時查找;

使用方法:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

fing [OPTIONS] [查找起始路徑] [查找條件] [處理動作]

查找起始路徑:指定具體搜索目標起始路徑;默認當前目錄;

查找條件:指定的查找標准,可以根據文件名,大小,類型,從屬關系,權限等標准,默認為指定目錄下的所有條件

處理動作:對符合條件的文件作出的操作,例如刪除等操作,默認為輸出至標准輸出

查找條件說明: 

以表達式的形式,包含選項和測試條件

  測試:結果通常為布爾型數據("true""fales")

   (1)根據文件名查找 注意:支持glob風格的通配符

    -name "pattern":區分大小寫

    -iname "pattern":不區分名字的大小寫,

    -regex "patten":基於正則表達式模式查找文件,匹配是整個路徑 ,而非其名;

           

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15         [root@linuxidc ~]# find /etc -iname "passwd"         /etc/passwd         /etc/pam.d/passwd         [root@linuxidc ~]# find /etc -iname "*passwd"         /etc/passwd         /etc/pam.d/passwd         /etc/security/opasswd         [root@linuxidc ~]# find /etc -iname "passwd*"         /etc/passwd         /etc/passwd-         /etc/pam.d/passwd         [root@linuxidc ~]# find /etc -iname "passwd?"         /etc/passwd-         [root@linuxidc ~]# find /etc -iname "passwd[[:place:]]"         [root@linuxidc ~]#

 

 

   (2)根據文件從屬關系查找

      -user username:查找屬主指定用戶的所有文件;

      -group groupname: 查找屬組指定用戶的所有文件;

     -uid UID:查找屬主指定UID的所有文件;

     -gid GID:查找屬組指定GID的所有文件;        #find /etc  -gid 5000

     -nouser:查找沒有屬主的文件                     #find /etc  -nouser

     -ngroup:查找沒有屬組的文件                    #find /etc  -nogroup

 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25        [root@linuxidc ~]# find /var/tmp/ -user CentOS          #屬主查找         /var/tmp/test/a.centos         /var/tmp/test/b.centos                 [root@linuxidc ~]# find /var/tmp/ -group mygrp -ls      #屬組查找         drwxrwsr-t   2 root     mygrp          66 Jul 31 04:43 /var/tmp/test         -rw-rw-r--   1 centos   mygrp          10 Jul 31 04:43 /var/tmp/test/b.centos         -rw-rw-r--   1 Fedora   mygrp           0 Jul 31 04:43 /var/tmp/test/b.fedora                  [root@linuxidc ~]# id  centos                            #UID查找         uid=1001(centos) gid=1001(centos) groups=1001(centos),1003(mygrp)         [root@linuxidc ~]# find /var/tmp/ -uid 1001         /var/tmp/test/a.centos         /var/tmp/test/b.centos                    [root@linuxidc ~]# tail -n2 /etc/group                  #GID查找         mygrp:x:1003:centos,fedora         hodoop:x:1004:         [root@linuxidc ~]# find /var/tmp/ -gid 1003          /var/tmp/test         /var/tmp/test/b.centos         /var/tmp/test/b.fedora                    [root@linuxidc ~]# find /etc  -nouser                #沒有屬主         [root@linuxidc ~]# find /etc  -nogroup                #沒有屬組

   (3)根據文件類型查找

     -trpe TYPE: 組合動作-ls使用,查看文件的詳細信息

      f:普通文件

      d:目錄   

      l:鏈接文件

      b:塊設備文件

      c:字符設備文件

      s:套接字文件

      p:管道文件

      組合測試:

      與: -a 默認組合操作邏輯:  二者同時成立

      或: -o  符合其中一項即可

      非: -not 或"!"  取反      

       !A -a !B=!(A -o B)

       !A -o !B=!(A -a B)

   練習: 找出/etc/下 屬主非root的文件,且文件名中不包含fstab    

1 2 3 4 [root@linuxidc ~]# find /etc/   !  -user root -ls -a -not -name "fatab" 1233465    0 drwx------   2 polkitd  root           63 7月 19 19:07 /etc/polkit-1/rules.d 34785013    8 -rw-------   1 tss      tss          7046 11月 21  2015 /etc/tcsd.conf 35135850    0 drwx--x--x   2 sssd     sssd            6 11月 20  2015 /etc/sssd

 

   (4)根據文件大小查找 

     -siza[+|-]#UNIT: 常用單位:k,m,g

      #UNIT: (#-1,#]     等於數字

      -#UNIT:[0,#-1)   小於數字

      +#UNIT:(#,00)    大於數字

      -empty: 查找空文件。

1 2 3 4 5 6 7 8 9 [root@linuxidc var]# find /etc/ -size -4k -a -exec ls -lh {} \; -rw-r--r--. 1 root root 465 7月  19 18:59 /etc/fstab -rw-------. 1 root root 0 7月  19 18:59 /etc/crypttab lrwxrwxrwx. 1 root root 17 7月  19 18:59 /etc/mtab -> /proc/self/mounts -rw-r--r-- 1 root root 228 7月  30 18:31 /etc/resolv.conf    [root@linuxidc usr]# find /etc/ -size 4k -a -exec ls -lh {} \;    [root@linuxidc usr]# find /etc/ -size 4k -a -exec ls -lh {} \;

   (5)根據時間戳查找

     以天為單位

       -atime   [+|-]# : [#,#-1] 

       -mtime                           

       -ctime

    以分鐘為單位

       -amin

       -mmin

       -cmin

   (6)根據權限查找

     -perm [/|-]mode:

        mode:精確查找 -perm -664 文件權限正好符合mode(mode為文件權限的八進制表示)。

       /mode:任何一類用戶(u,g,o)權限中的任何一位(rwx)符合條件即滿足  文件權限部分符合mode

         9位權限之間存在"或"關系

       -mode:每一類用戶(u,g,o)的權限中的每一位(e,w,x)同時符合條件即滿足;

         9位權限之間存在"與"關系文件權限完全符合mode。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 [root@linuxidc ~]# find . -perm 644 ./.bash_logout ./.bashrc ./.cshrc ./.tcshrc    [root@linuxidc ~]# find . -perm -644 ./.bash_logout ./.bashrc ./.cshrc ./.tcshrc ./.cache/abrt ./.cache/abrt/applet_dirlist    [root@linuxidc ~]# find /tmp/ -perm /222  /tmp/ /tmp/log /tmp/log/tallylog /tmp/log/lastlog

 

處理動作

 -print:輸出至標准輸出,默認的動作

 -ls:類似於對查找到的文件執行"ls -l"命令,輸出文件的詳細信息.

 -delete:刪除查找到的文件;

 -fls /path/to/SomeFIle:把查找到的所有文件的長格式信息保存至指定文件中

 -ok COMMAND {} \;  :對查找的每個文件執行有COMMAND表示的命令;每次操作都有用戶進行確認;

 -exec COMMAND {} \; :對查找的每個文件執行有COMMAND表示的命令;

   注意: find傳遞查找到的文件路徑至後面的命令時,是先查找出所有符合條件的文件路徑,並一次性傳遞給後面的命令;

   但是有些命令不能接受過長的參數,此時命令執行會失敗;另一種方式可規避此問題.

      find | xargs COMMAND       學習xargs命令

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@linuxidc ~]# find /tmp -name "qwe.sh"  -type f -delete [root@linuxidc ~]# ls /tmp/ hogsuspend  log     [root@linuxidc ~]# find /etc/ ! -perm /222  -type f -fls /tmp/qwe.sh [root@linuxidc ~]# cat /tmp/qwe.sh 33885564  196 -r--r--r--   1 root     root       198453 7月 19 19:02 /etc/pki/ca-trust/extracted/java/cacerts 67923558  352 -r--r--r--   1 root     root       359773 7月 19 19:02 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt    [root@linuxidc ~]# find /etc/ ! -perm /222  -type f -ok ls -lh {} \; ls ... /etc/pki/ca-trust/extracted/java/cacerts > ? y -r--r--r--. 1 root root 194K 7月  19 19:02 /etc/pki/ca-trust/extracted/java/cacerts ls ... /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt > ? y -r--r--r--. 1 root root 352K 7月  19 19:02 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt ls ... /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem > ? y    [root@linuxidc ~]# find /etc/ ! -perm /222  -type f -exec ls -lh {} \; -r--r--r--. 1 root root 194K 7月  19 19:02 /etc/pki/ca-trust/extracted/java/cacerts -r--r--r--. 1 root root 352K 7月  19 19:02 /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt -r--r--r--. 1 root root 261K 7月  19 19:02 /etc/pki/ca-trust/extracted/pem/tls-

 

xargs命令:

    該命令的主要功能是從輸入中構建和執行shell命令。       
     在使用find命令的-exec選項處理匹配到的文件時,   find命令將所有匹配到的文件一起傳遞給exec執行。但有些系統對能夠傳遞給exec的命令長度有限制,這樣在find命令運行幾分鐘之後,就會出現 溢出錯誤。錯誤信息通常是“參數列太長”或“參數列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。  
    find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項那樣。這樣它可以先處理最先獲取的一部分文件,然後是下一批,並如此繼續下去。  
    在有些系統中,使用-exec選項會為處理每一個匹配到的文件而發起一個相應的進程,並非將匹配到的文件全部作為參數一次執行;這樣在有些情況下就會出現進程過多,系統性能下降的問題,因而效率不高;  
    而使用xargs命令則只有一個進程。另外,在使用xargs命令時,究竟是一次獲取所有的參數,還是分批取得參數,以及每一次獲取參數的數目都會根據該命令的選項及系統內核中相應的可調參數來確定。
    #查找當前目錄下的每一個普通文件,然後使用xargs命令來測試它們分別屬於哪類文件。 

1 2 3 4 5 root@linuxidc ~]# find . -type f -print | xargs file     ./users2:        ASCII text     ./datafile3:      empty     ./users:          ASCII text     ./test.tar.bz2: bzip2 compressed data, block size = 900k

   #回收當前目錄下所有普通文件的執行權限。

1 2 3 4 5 6 7 8 [root@linuxidc ~]# find . -type f -print|xargs chmod a-x [root@linuxidc ~]# ll  總用量 8 -rw-r--r-- 1 root root    0 7月  30 11:38 ad -rwSr--r-- 1 root root 2620 7月  24 10:10 passwd.bak drwxr-xr-x 2 root root    6 7月  30 12:31 qwe drwxr-xr-x 2 root root 4096 7月  29 20:10 shell drwxr-xr-x 2 root root  125 7月  30 16:02 shlianxi

    #在當面目錄下查找所有普通文件,並用grep命令在搜索到的文件中查找hostname這個詞 

1 [root@linuxidc ~]# find /etc -type f -print | xargs grep "hostname"

練習:

1.查找/var/目錄下屬主為root,且數組為mall的所有文件或目錄;   

1 2 3 [root@linuxidc ~]# find /var/ -user root -a -group mail /var/spool/mail /var/spool/mail/root

   2.查找/usr目錄下不屬於root,bin,或Hadoop的所有文件或目錄,用兩種方法

[root@linuxidc ~]# find /usr/ -not \( -user root -o -user bin -o -user hadoop\)

[root@linuxidc ~]# find /usr/ -not -user root -a -not -user bin -a not -usr hadoop

3.查找/etc/目錄下最近一周內其文件修改過,且屬主不是root用戶也不是hadoop用戶的文件或目錄 

1 [root@linuxidc ~]# find / -mtime -7 -a ! -user root -a ! -user hadoop

4.查找當前系統上沒有屬主或屬組,且最近一周內曾被訪問的文件或目錄

1 2 [root@linuxidc ~]# find / -nouser -o -nogroup -a -atime -7 [root@linuxidc ~]#find / \(-nouser -o -nogroup\) -atime -7 -ls

5,查找/etc/目錄下大於1M且類型為普通文件的所有文件  

1 2 [root@linuxidc ~]# find  /etc/ -size +1M -a -type f -ls [root@linuxidc ~]# find /etc/ -size +1M  -type f -exec ls -lh {} \;

6,查找/etc/目錄下所有用戶都沒有寫權限的文件

1 [root@linuxidc ~]# find  /etc/ -size +1M -a -type f -ls

7.查找/etc/目錄下至少有一類用戶沒有執行權限的文件      

1 [root@linuxidc ~]# find /etc/ !  -perm -111 -type -f  -ls

8.查找/etc/init.d目錄下,所有用戶都有執行權限,且其他用戶有寫權限的所有文件. 

1 2  [root@linuxidc ~]#find /etc/ -perm -111 -a -perm  -002  -type f -ls  [root@linuxidc ~]#find /etc/ -perm -113 -type f -ls

Linux find 命令用法總結  http://www.linuxidc.com/Linux/2015-04/116854.htm

Linux下的文件查找命令——find  http://www.linuxidc.com/Linux/2016-05/131826.htm

Linux下查找文件find命令  http://www.linuxidc.com/Linux/2014-10/108575.htm

Linux下find命令詳解 http://www.linuxidc.com/Linux/2011-08/40669.htm

文本查找利器find的使用 http://www.linuxidc.com/Linux/2014-03/97559.htm

功能強大的find命令 http://www.linuxidc.com/Linux/2014-01/95236.htm

Linux系統find命令詳解 http://www.linuxidc.com/Linux/2014-06/103232.htm

Copyright © Linux教程網 All Rights Reserved