linux中檔案尋找工具find命令用法

來源:互聯網
上載者:User


檔案尋找 :      

在營運人員作業系統時,要接觸大量的檔案,為了避免忘記檔案存放位置的尷尬,就需要我們有一種檔案尋找工具的幫忙,下面是兩個檔案尋找工具的詳解,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:基於基本Regex寫匹配模式

[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":基於Regex模式尋找檔案,匹配是整個路徑 ,而不僅僅是其名成;  find -regex ".*\.sh$"

     [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

     [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 

[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: 尋找空檔案。

[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才 會匹配

[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命令

[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命令來測試它們分別屬於哪類檔案。 

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
#回收目前的目錄下所有普通檔案的執行許可權。

[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這個詞

[root@wen-7 ~] # find /etc -type f -print | xargs grep "hostname"
實戰演練:

1.尋找/var/目錄下屬主為root,且數組為mall的所有檔案或目錄;

[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使用者的檔案或目錄

[root@w7 ~] # find / -mtime -7 -a ! -user root -a ! -user hadoop
4.尋找當前系統上沒有屬主或屬組,且最近一周內曾被訪問的檔案或目錄
[root@wen-7 ~] # find / -nouser -o -nogroup -a -atime -7
[root@wen-7 ~] #find / \(-nouser -o -nogroup\) -atime -7 -ls
5,尋找/etc/目錄下大於1M且類型為普通檔案的所有檔案 

[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/目錄下所有使用者都沒有寫入權限的檔案

[root@wen-7 ~] # find  /etc/ ! -perm /222
7.尋找/etc/目錄下至少有一類使用者沒有執行許可權的檔案     

[root@w7 ~] # find /etc/ !  -perm -111 -type f  -ls
8.尋找/etc/init.d目錄下,所有使用者都有執行許可權,且其他使用者有寫入權限的所有檔案.

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.