Linux基礎--文件查找

1. which

which - shows the full path of (shell) commands.

[root@localhost ~]# which top
/usr/bin/top
[root@localhost shell]# which grep
alias grep='grep --color=auto'
/usr/bin/grep
[root@localhost ~]# which --skip-alias grep
/usr/bin/grep


2. locate

locate - find files by name

[root@localhost ~]# locate passwd
/etc/passwd
/etc/passwd-
/etc/pam.d/passwd
/etc/security/opasswd
/root/.pyenv/versions/3.5.1/lib/python3.5/test/keycert.passwd.pem
/root/.pyenv/versions/3.5.1/lib/python3.5/test/ssl_key.passwd.pem
/usr/bin/gpasswd
/usr/bin/grub2-mkpasswd-pbkdf2
/usr/bin/kdepasswd
/usr/bin/lppasswd
/usr/bin/passwd
/usr/bin/smbpasswd
/usr/bin/vncpasswd
...


locate文件查找是基於數據庫的,系統會每天固定時間去更新locate數據庫,那麼當天創建的文件可能使用locate命令就沒有辦法找到了。此時需要手動更新locate數據庫,可以使用系統自帶的每日更新locate數據庫腳本

[root@localhost ~]# /etc/cron.daily/mlocate

或者

[root@localhost ~]# updatedb


3. find

find查找文件是實時查找,沒有基於數據庫,速度可能會慢一點,但是比locate會更常用。因爲locate會遍歷整個文件系統,然後建立數據庫索引,這會消耗大量的資源,影響服務器上服務的正常運行。


基本用法可用通過此[鏈接]找到,介紹得很詳細了,這裏只是總結一下查找方式和一些應該注意的地方。

  • 根據文件名查找

可以使用glob匹配規則,可以使用正則表達式。

[root@localhost ~]# find /etc/ -name *.conf
[root@localhost ~]# find /etc/ -regextype posix-basic -regex "/etc/sudo.*\.conf"


regextype爲正則表達式類型,默認爲emacs類型,還有posix-awk, posix-basic, posix-egrep,posix-extended這四種。

Changes the regular expression syntax understood by -regex  and  -iregex
tests  which  occur  later  on  the command line.  Currently-implemented
types are emacs (this is the default),  posix-awk,  posix-basic,  posix-
egrep and posix-extended.

當你給出不存在的regextype時,find命令會給出如下提示信息,可用的類型有這些。不太清楚是man手冊沒有更新,還是這些可用的類型中其中一些只是標準類型的縮寫。不過常用的類型一般也就是posix-basic和posix-extended吧。

valid types are ‘findutils-default’, ‘awk’, ‘egrep’, ‘ed’, ‘emacs’, ‘gnu-awk’, ‘grep’, ‘posix-awk’, ‘posix-basic’, ‘posix-egrep’, ‘posix-extended’, ‘posix-minimal-basic’, ‘sed’.


值得注意的是man手冊中這樣一段描述,意思是在find中使用正則表達式需要匹配整個路徑而不是僅僅匹配文件,如果僅匹配文件名你是找不到的。

File  name  matches  regular expression pattern.  This is a match on the
whole  path,  not  a  search.   For  example,  to  match  a  file  named
'./fubar3', you can use the regular expression '.*bar.' or '.*b.*3', but
not 'f.*r3'.  The regular expressions understood by find are by  default
Emacs  Regular  Expressions, but this can be changed with the -regextype
option.

感受一下

[root@localhost ~]# find /etc/ -regextype posix-basic -regex "sudo.*\.conf"
[root@localhost ~]# find /etc/ -regextype posix-basic -regex "/etc/sudo.*\.conf"
/etc/sudo-ldap.conf
/etc/sudo.conf
  • 根據文件類型查找

  • 根據文件時間戳查找

  • 根據文件大小查找

  • 根據文件權限查找

mode精確匹配則滿足條件

[root@localhost ~]# find /etc/ -user root -perm 400 -ls


將mode轉換成二進制,比如mode=111那麼轉換成二進制爲001 001 001,而被查找的文件的權限位也可以被轉換成一個二進制數,兩者在位上爲1的部分必須完全匹配,則滿足條件,位上爲0的不用管。即被查找文件需要完全匹配mode給出的基本權限即可。

[root@localhost ~]# find /etc/ -user root -perm -111 -ls


將mode轉換成二進制,比如mode=111那麼轉換成二進制爲001 001 001,而被查找的文件的權限位也可以被轉換成一個二進制數,兩者在任意一位爲1位上匹配,則滿足條件,位上爲0的不用管。即被查找文件只需要匹配mode給出的基本權限的任意一個權限位即可。

[root@localhost ~]# find /etc/ -user root -perm /111 -ls
  • 多條件聯合查找時使用與或非

注意:優先級:非>與>或;儘量使用括號將條件之間的邏輯關係描述清楚,增強可讀性。

  • 對於查找到的文件進行處理

雖然find自帶兩個選項-ok和-exec來做複雜處理動作,但是它是將所有匹配的結果一次性交給後面的處理動作,當結果集過大時很容易出現問題。

[root@localhost ~]# find /etc -user root -o -name "*.conf" -size +1k | wc -l
2016
[root@localhost ~]# find /etc -user root -o -name "*.conf" -size +1k -exec ls -dl {} \; | wc -l
1

使用xargs命令結合管道來使用就可以解決。

[root@localhost ~]# find /etc -user root -o -name "*.conf" -size +1k | xargs ls -dl | wc -l
2016


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章