天天看點

Linux檔案查找指令之locate與find

作者:小時候裸奔是多麼潇灑
locate與find是經常使用的Linux 指令,剛接觸Linux時對這兩個指令的使用傻傻的分不清。現在我們來對比一下兩個指令到底有哪些差別。

locate

locate 讓使用者可以很快速的搜尋檔案系統内是否有指定的檔案。其方法是先建立一個包括系統内所有檔案名稱及路徑的資料庫,之後當尋找時就隻需查詢這個資料庫,而不必實際深入檔案系統之中了。在一般的 distribution 之中,資料庫的建立都被放在 crontab 中自動執行。

1. 指令格式:

Locate [選擇參數] [樣式]           

2.指令功能:

locate指令可以在搜尋資料庫時快速找到檔案,locate為模糊查找,資料庫由updatedb程式來更新,updatedb是由cron daemon周期性建立的,locate指令在搜尋資料庫時比由整個由硬碟資料來搜尋資料來得快,但較差勁的是locate所找到的檔案若是最近才建立或 剛更名的,可能會找不到,在内定值中,updatedb每天會跑一次,可以由修改crontab來更新設定值。(etc/crontab)

locate指定用在搜尋符合條件的檔案,它會去儲存檔案與目錄名稱的資料庫内,locate查詢檔案時,會去搜尋/var/lib/mlocate/mlocage.db,尋找合乎範本樣式條件的檔案或目錄錄,可以使用特殊字元(如”*” 或”?”等)來指定範本樣式,如指定範本為kcpa*ner, locate會找出所有起始字串為kcpa且結尾為ner的檔案或目錄,如名稱為kcpartner若目錄錄名稱為kcpa_ner則會列出該目錄下包括 子目錄在内的所有檔案。

locate指令和find找尋檔案的功能類似,但locate是透過update程式将硬碟中的所有檔案和目錄資料先建立一個索引資料庫,在 執行loacte時直接找該索引,查詢速度會較快,索引資料庫一般是由作業系統管理,但也可以直接下達update強迫系統立即修改索引資料庫。

3.指令參數:

-e 将排除在尋找的範圍之外。

-1 如果 是 1.則啟動安全模式。在安全模式下,使用者不會看到權限無法看到 的檔案。這會始速度減慢,因為 locate 必須至實際的檔案系統中取得檔案的權限資料。

-f 将特定的檔案系統排除在外,例如我們沒有到理要把 proc 檔案系統中的檔案 放在資料庫中。

-q 安靜模式,不會顯示任何錯誤訊息。

-n 至多顯示 n個輸出。

-r 使用正規運算式 做尋找的條件。

-o 指定資料庫存的名稱。

-d 指定資料庫的路徑

-h 顯示輔助訊息

-V 顯示程式的版本訊息

4.使用執行個體:

執行個體1:搜尋etc目錄下所有以sh開頭的檔案

[root@CentOS7 ~#]locate /etc/sh       
/etc/shadow
/etc/shadow-
/etc/shells
[root@centos7 ~#]locate -r "/etc/\sh "  # 正則,錨定詞首
/etc/shadow
/etc/shadow-
/etc/shells
[root@centos7 ~#]           

執行個體2:忽略大小寫

[root@centos7 ~#]locate -i ~/d
/root/Desktop/root/Documents/root/Downloads
/root/d1
/root/dd
/var/lib/pcp/pmdas/root/domain.h
[root@centos7 ~#]           

執行個體3:更新資料庫

[root@centos7 ~#]locate ~/a
/root/anaconda-ks.cfg
[root@centos7 ~#]updatedb
[root@centos7 ~#]locate ~/a
/root/a.sh
/root/anaconda-ks.cfg
[root@centos7 ~#]
           

find

1. 主要用途:

find指令是一個實時查找工具,通過周遊指定路徑而完成對檔案的查找;在使用該指令時,如果不標明參數,則在目前目錄下查找子目錄與檔案并顯示之;另外,任何位于參數之前的字元串,都将視為欲查找的目錄名。由于是實時周遊查找,find有如下特性:精确實時查找,速度慢可能隻搜尋使用者具備讀取和執行權限的目錄。

2.find文法:

find [OPTION]... [查找路徑] [查找條件] [處理動作]

查找路徑:指定具體目标路徑,預設為目前目錄

查找條件:指定的查找标準,可以是檔案名、大小、類型、權限等标準進行;預設為找出指定路徑下的所有檔案

處理動作:對符合條件的檔案做操作,預設輸出至螢幕

3.查找條件:

1. 根據檔案名和inode查找

2. 根據屬主、屬組查找

3. 根據檔案類型查找

4. 根據邏輯組合條件查找

5. 根據檔案大小來查找

6. 根據時間戳來查找

7. 根據權限來查找

4.處理動作:

1. -print: 預設動作,顯示至螢幕

2. -ls: 類似于對查找到的檔案執行 ls -l 指令

3. -delete: 删除查找到的檔案

4. -fls file: 查找到的所有長格式的資訊儲存至指定檔案中

5. -ok COMMMAND {} \; 對查找到的每個檔案執行由COMMAND指定的指令,且都會互動式要求使用者确認

6. -exec COMMAND {} \; 對查找到的每個檔案執行由COMMAND指定的指令;

7. {}: 用于引用查找至的檔案名稱自身

8. find 傳遞查找到的檔案至後面指定的指令時,查找到所有符号條件的檔案一次性傳遞給後面的指令

9. 有些指令不能接受過多的參數,此時指令執行可能會失敗,用 xargs 來規避此問題

find |xargs COMMAND

5.常用參數:

檔案名和inode類:

-name "檔案名稱": 支援使用glob, *, ?, [], [^]

    -iname "檔案名稱": 不區分字母大小寫

    -inum n: 按inode号查找

    -somefile name: 相同的inode号檔案

    -links n: 連結數為n的檔案

    -regex "PATTERN": 以PATTERN比對整個檔案路徑字元串,而不僅僅是檔案名稱           

屬主屬組類:

-user USERNAME: 查找屬主為指定使用者(UID)的檔案

    -group GROUPNAME: 查找屬組為指定組(GID)的檔案

    -uid UserID: 查找屬主為指定的UID号的檔案

    -gid GroupID: 查找屬組為指定的GID号的檔案

    -nouser: 查找沒有屬主的檔案

    -nogroup: 查找沒有屬組的檔案           

檔案類型類:

b      block (buffered) special

c      character (unbuffered) special

d      directory

p      named pipe (FIFO)f      regular file

l      symbolic  link

s      socket           

邏輯組合條件類:

組合條件:

與:-a
或:-o
非:-not, !
           

摩根定律:

(非P) 或(非Q) = 非(P且Q)
(非P) 且 (非Q) = 非(P或Q)
           
Linux檔案查找指令之locate與find
Linux檔案查找指令之locate與find

檔案大小類:

-size [+|-]#UNIT
    常用機關:k,M,G 
#UNIT: (#-1,#]
    如:5M 表示 (4M,5M]
-#UNIT: [0,#-1]
    如:-5M 表示 [0,5M]
+#UNIT: (#,oo)
    如:+5M 表示 (6M,oo)           

關于檔案大小類的解釋:為什麼-size 5M 還是找精确的5M而是表示(4M,5M], 試想檔案的大小指什麼?是指檔案資料的大小還是包括了中繼資料後的大小,那你找中繼資料的大小有意義嗎?但檔案的大小肯定是包含中繼資料大小的,而我們一般以檔案大小找檔案時往往考慮的是檔案資料的大小;另外,精确查找一定大小的檔案意義不大;是以這裡的大小會有1個機關的浮動。

時間戳類:

以”天”為機關:
    -atime [+|-]#        
        #: [#,#+1)
        +#: [#+1,oo)        
        -#: [0,#)
    -mtime    
    -ctime

以“分鐘”為機關:
    -amin    
    -mmin    
    -cmin           
Linux檔案查找指令之locate與find

關于時間戳類的解釋:為什麼-atime 3 表示的是 [3,4),這個就很好解釋了,我們這兒所說的時間是指時間段而非時刻,一“天”與一“分鐘”都是指一個時間段,隻有[3,4)這個半閉半開的區間才能完整地表示第三天。

權限類:

-perm [/|-]MODE
    MODE: 精确比對權限
    /MODE: 任何一類(u,g,o)對象的權限中隻要能一位比對即可,屬于或關系。以前用'+',CentOS 7以'/'替代之
    -MODE: 每一類對象都必須同時擁有指定權限,屬于與關系 
    0:表示不關注           

示例:

find -perm 644 表示要嚴格比對644的檔案
find -perm +222 表示u,g,o任何一類使用者有寫權限即比對
find -perm -222 表示僅嚴格比對寫權限,即每個使用者必須要有寫權限
find -perm -002 表示僅嚴格比對other使用者的寫權限           

6.使用示例:

執行個體1:将配置檔案備份到指定目錄下并添加擴充名.org

[root@localhost ~]# find . -name "*.conf" -exec cp -r {} /testdir/{}.org \; 
[root@localhost ~]# cd /testdir/
[root@localhost testdir]# ls
a.conf.org  b.conf.org
[root@localhost testdir]#           

執行個體2:.提示删除存在時間超過3天以上的屬主為young的臨時檔案

[root@localhost ~]# find /tmp -ctime +3 -user young -exec rm -fr {} \;
[root@localhost ~]#           

執行個體3:在主目錄中查找可被其它使用者寫入的檔案

[root@localhost ~]# find ~ -perm -002
/root/num
[root@localhost ~]# find ~ -perm -002 -exec chmod o-w {} \;
[root@localhost ~]# ll num
--w--w---- 1 root root 35 Jan 21 05:55 num           

執行個體4:查找/var目錄下屬主為root,且屬組為mail的所有檔案

[root@localhost ~]# find /var -user root  -group mail -ls #預設關系就是與
1179652    4 drwxrwxr-x   2 root     mail         4096 Jan 23 11:04 /var/spool/mail           

執行個體5:查找/var目錄下不屬于root、lp、gdm的所有檔案

[root@localhost ~]# find /var ! -user root ! -user lp ! -user gdm           

執行個體6:查找/var目錄下最近一周内其内容修改過,同時屬主不為root,也不是postfix的檔案

[root@localhost ~]# find /var/ -mtime -7 ! -user root ! -user postfix -ls
1179676    4 drwx------   3 daemon   daemon       4096 Jan 23 11:04 /var/spool/at
524399    4 drwx------   2 nginx    nginx        4096 Jan 23 03:16 /var/log/nginx
524413    0 -rw-r--r--   1 nginx    root            0 Jan 23 03:16 /var/log/nginx/access.log
524391    0 -rw-r--r--   1 nginx    root            0 Jan 21 03:44 /var/log/nginx/error.log
132174    4 drwx------   3 nginx    nginx        4096 Jan 21 03:44 /var/lib/nginx
132175    4 drwx------   7 nginx    nginx        4096 Jan 21 03:44 /var/lib/nginx/tmp
132173    4 drwx------   2 nginx    root         4096 Jan 21 03:44 /var/lib/nginx/tmp/client_body
132219    4 drwx------   2 nginx    root         4096 Jan 21 03:44 /var/lib/nginx/tmp/proxy
132221    4 drwx------   2 nginx    root         4096 Jan 21 03:44 /var/lib/nginx/tmp/uwsgi
132222    4 drwx------   2 nginx    root         4096 Jan 21 03:44 /var/lib/nginx/tmp/scgi
132220    4 drwx------   2 nginx    root         4096 Jan 21 03:44 /var/lib/nginx/tmp/fastcgi           

執行個體7:查找目前系統上沒有屬主或屬組,且最近一個周内曾被通路過的檔案

[root@bash ~]# find / -nouser -o -nogroup -a -atime -7           

執行個體8:查找/etc目錄下大于1M且類型為普通檔案的所有檔案

[root@bash ~]# find /etc/ -size +1M -type f
/etc/selinux/targeted/policy/policy.29
/etc/udev/hwdb.bin           

執行個體9:查找/etc目錄下所有使用者都沒有寫權限的檔案

[root@bash ~]# find /etc/ ! -perm /222
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
/etc/lvm/profile/cache-mq.profile
/etc/lvm/profile/cache-smq.profile
/etc/lvm/profile/command_profile_template.profile
/etc/lvm/profile/metadata_profile_template.profile
/etc/lvm/profile/thin-generic.profile
/etc/lvm/profile/thin-performance.profile
/etc/openldap/certs/password
/etc/gshadow
/etc/dbus-1/system.d/cups.conf
/etc/shadow
/etc/gshadow-
/etc/ld.so.conf.d/kernel-3.10.0-327.el7.x86_64.conf
/etc/shadow-
/etc/udev/hwdb.bin
/etc/machine-id
/etc/pam.d/cups
/etc/sudoers           

執行個體10:查找/etc目錄下至少有一類使用者沒有執行權限的檔案

[root@bash ~]# find /etc/ ! -perm -111 # 至少有一類使用者沒有就是所有使用者都沒有           

執行個體11:.查找/etc/init.d目錄下,所有使用者都有執行權限,且其它使用者有寫權限的檔案

[root@bash ~]# find /etc/init.d -perm -113
/etc/init.d           

或者

[root@bash ~]# find /etc/init.d -perm -111 -perm -002
/etc/init.d           

執行個體12:摩根定律找出/tmp目錄下,屬主不是root,且檔案名不以f開頭的檔案

[root@centos7 ~]#find /tmp \( -not -user root -a -not -name 'f*' \) -ls
即
[root@centos7 ~]#find /tmp -not \( -user root -o -name 'f*' \) -ls           

執行個體13:查找/etc/下,除/etc/sane.d目錄的其它所有.conf字尾的檔案

[root@bash ~]# find /etc -path '/etc/sane.d' -prune -o -name '*.conf'           

執行個體14:比對檔案路徑或檔案

[root@bash ~]# find /usr/ -path '*local'
/usr/bin/abrt-action-analyze-ccpp-local
/usr/share/doc/postfix-2.10.1/examples/qmail-local
/usr/share/aclocal
/usr/libexec/postfix/local
/usr/local           

執行個體15:基于正規表達式比對檔案路徑

[root@bash ~]# find . -regex ".*txt#34;              
./.mozilla/firefox/4dqu966q.default/revocations.txt
./vimrc/spf13-vim/LICENSE.txt
./a.txt           

本文原創位址:https://www.linuxprobe.com/locate-and-find.html

繼續閱讀