系統基礎之文件查找工具find

文件查找:       

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


第一款工具: Locate

locate – find files by name

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

索引庫位置:/var/lib/mlocate/mlocate.db

索引庫的構建:

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

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

    updatedb       – update a database for mlocate;

工作特性:

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

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

使用方法:

  locate [OPTIONS] FILE..

   選項:

    默認命令: 搜索帶關鍵字的文件

    -i:忽略大小寫

    -n #:只列舉搜索結果的前#個

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
[root@wen-7 ~]# locate -c  "passwd"
165
[root@wen-7 ~]# locate -c  "inittab"
4
 
[root@wen-7 ~]# 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@wen-7 ~]# 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")

  默認多條件并行執行,相當 -a 與的關系

   

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

     -name "pattern":區分大小寫 

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

        -inum  n:按inode號查找             find   -inum 21321321

        -samefile name : 相同inode號的文件      find -somefile aaa

        -links  n:鏈接數為n的文件    find -link   3

     -regex "patten":基于正則表達式模式查找文件,匹配是整個路徑 ,而不僅僅是其名成;  find -regex ".*\.sh$"

        wKioL1eEFy2hLWJ4AABrgXSwqs8040.png   

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



   (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@w7 ~]# find /var/tmp/ -user centos          #屬主查找
        /var/tmp/test/a.centos
        /var/tmp/test/b.centos
       
       [root@w7 ~]# 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@w7 ~]# id  centos                            #UID查找
        uid=1001(centos) gid=1001(centos) groups=1001(centos),1003(mygrp)
        [root@w7 ~]# find /var/tmp/ -uid 1001
        /var/tmp/test/a.centos
        /var/tmp/test/b.centos
         
        [root@w7 ~]# tail -n2 /etc/group                  #GID查找
        mygrp:x:1003:centos,fedora
        hodoop:x:1004:
        [root@w7 ~]# find /var/tmp/ -gid 1003 
        /var/tmp/test
        /var/tmp/test/b.centos
        /var/tmp/test/b.fedora
         
        [root@wen-7 ~]# find /etc  -nouser                #沒有屬主
        [root@wen-7 ~]# find /etc  -nogroup                #沒有屬組


   (3)根據文件類型查找

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

      f:普通文件

       d:目錄    

      l:鏈接文件

      b:塊設備文件

      c:字符設備文件

      s:套接字文件

      p:管道文件

    例: find / -name "*int*" -type d -ls

         find /  -type b

   組合測試:

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

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

         非: -not 或"!"  取反  

                        德*摩根定律:

                                非A 或  非B = 非(A 且 B)

                                非A 且  非 B= 非(A 或 B)   

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

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


    排除某目錄:

        find / etc/ -path /etc/bin -a -prune  -o name "*.sh"  -print

     把/etc/bin裁掉不搜索,在剩下的目錄里找名字為.sh文件

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

1
2
3
4
[root@wen-7 ~]# 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@wen-7 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@wen-7 usr]# find /etc/ -size 4k -a -exec ls -lh {} \;
 
[root@wen-7 usr]# find /etc/ -size 4k -a -exec ls -lh {} \;


      

   (5)根據時間戳查找

     以天為單位

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

                    7 [ 7,8]        [#,#+1]

                    -7 [ 0.7]      [0,#]

                            +7 [ 8,+∞]  [#+1,∞]

       -mtime                            

       -ctime

    以分鐘為單位

       -amin

       -mmin

       -cmin

   (6)根據權限查找

     -perm [/|-]mode:

        mode:精確查找 -perm -664 文件權限正好符合mode(mode為文件權限的八進制表示)。0表示不關系其所在位的權限

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

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

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

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

只要當任意人有寫權限時, find -perm +222就會匹配

? 只有當每個人都有寫權限時, find -perm -222才會匹配

? 只有當其它人( other)有寫權限時, find -perm -002才會匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@wen-7 ~]# find . -perm 644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
 
[root@wen-7 ~]# find . -perm -644
./.bash_logout
./.bashrc
./.cshrc
./.tcshrc
./.cache/abrt
./.cache/abrt/applet_dirlist
 
[root@wen-7 ~]# 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 -perm -222 -exec cp {} {}.bak \;

   注意: 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@wen-7 ~]# find /tmp -name "qwe.sh"  -type f -delete
[root@wen-7 ~]# ls /tmp/
hogsuspend  log
  
[root@wen-7 ~]# find /etc/ ! -perm /222  -type f -fls /tmp/qwe.sh
[root@wen-7 ~]# 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@wen-7 ~]# 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@wen-7 ~]# 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@wen-7 ~]# 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@wen-7 ~]# find . -type f -print|xargs chmod a-x
[root@wen-7 ~]# 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@wen-7 ~]# find /etc -type f -print | xargs grep "hostname"

 




實戰演練:

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

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

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

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

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

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

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

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

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

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

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

1
[root@wen-7 ~]# find  /etc/ ! -perm /222

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

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

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

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

                                                  

                                                    


原創文章,作者:wencx,如若轉載,請注明出處:http://www.www58058.com/36061

(0)
wencxwencx
上一篇 2016-08-16
下一篇 2016-08-16

相關推薦

  • 磁盤分區相關知識

    磁盤分區相關知識 使用磁盤分區的過程設備識別→設備分區→創建文件系統→標記文件系統→在/etc/fstab文件中創建條目→掛載新的文件系統 分區不是必須的,但是分區是必要的:優化I/O性能實現磁盤空間配額限制提高修復速度隔離系統和程序安裝多個OS采用不同文件系統 不重啟激活新添加的磁盤:echo “- – -” > …

    Linux干貨 2017-08-20
  • 配置基于mysql的虛擬用戶認證vsftp服務器

    1.安裝vsftp、mysql、epel_release(epel源,后面要安裝的pam_mysql認證模塊需要這個源)、pam_mysql 2.剛裝的mysql運行一下/usr/bin/mysql_secure_installation 3.建立數據庫vsftpd,建表user,     create database vsftpd; …

    Linux干貨 2016-06-22
  • N25-第三周作業

    N25第三周作業 博客作業 1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@ip-172-31-25-9 ~]# who|awk '{print $1}'|sort -u ec2-user [root@ip-172-31-25-9&n…

    Linux干貨 2017-02-06
  • NFS的應用實例

    實驗目的:通過NFS實現共享 實驗要求:實驗環境下防火墻以及selinux都是要關閉的?。?! 實驗環境:三臺虛擬機,以及相關安裝程序 實驗過程: 設置服務端centos 6-1 [root@centos6 ~]# yum install mysql-servernfs-utils httpd [root@centos6 ~]# service mysqld …

    2017-05-03
  • 操作系統概述

                                 操作系統概述 對于一個真正的計算機系統來說,我們可以說它是由硬件+軟件組成。沒有軟件的計算機,我們稱它為裸機。當然,它也干不成什么事。所以,要想使得計算…

    Linux干貨 2016-10-27

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-08-19 11:57

    文章對文件查找工具常見用法有了一個完整的介紹,文件查找工具中,find命令是最重要的,幾乎在所有的筆試中都可能·遇得到,所以需要多加練習,熟練掌握。

欧美性久久久久