1. 程式人生 > >檔案查詢命令find,locate,whereis,which

檔案查詢命令find,locate,whereis,which

一:find : 查詢檔案精確,但是速度慢,資源消耗高,通過遍歷硬碟來查詢

find可以從檔名、讀取修改時間、大小、型別等等來篩選檔案,可以完成日常幾乎所有的查詢需求。

1:基本語法:find [目錄] 條件  (如果不寫目錄,則預設從當前目錄查詢)

2:常用引數:

①:時間:-newer file  比file所指的檔案還要新的檔案

②:大小:-size

③:所有者、所屬組 -user name  所有者使用者名稱稱是name的檔案

                             -group name 所有者使用者組群名稱是name的檔案

④:許可權:-perm

3:例子:

①:find /tmp/ -name 'shadow*' 找到/tmp 目錄下所有以shadow開頭的檔案

②:find /tmp/ -name '*shadow*' 找到/tmp 目錄下所有包含shadow的檔案

③:find ./ -size +1M 找到當前目錄下大於1M的檔案

④:find ./ -perm +300 找到許可權有w或有x或有wx的

④:find ./ -perm -300 找到許可權必須有wx,即只能是wx或rwx

⑤:find ./ -type d 檔案型別為目錄的

⑥:find ./ ! -type d 檔案型別不為目錄的

⑦:find ./ -user student 所有人為student的檔案

⑧:find ./ -user student or -groupstudent 所有人或所屬組為student的檔案

⑨:find ./ -user student -groupstudent 所有人和所屬組都為student的檔案

4:特殊用法:再找到的檔案上執行命令

①:基本語法:find 目錄 條件–OK|-exec 命令 {}空格\ ;

②:例子:找到/etc、目錄下所有以.conf結尾的檔案,備份這些檔案給這些檔案加上.org的字尾。 find /etc/ -name “*.conf” –execcp {} {}.org \;

二:locate  有自己的資料庫,結合資料庫來查詢檔案的位置只適合快速查詢,不精確

1:基本語法:locate 檔案或者目錄名稱

2:例子:locatepasswd

3:注意:建立新檔案後需要更新locate的資料庫(/var/lib/mlocate/mlocate.db),用update命令,並不是所有的檔案都更新到資料庫,在/etc/updatedb.conf記錄了哪些內容不會被更新到資料庫中

三:whereis  whereis可以找到可執行命令和man page

1:基本語法: whereis [-bmsu] 檔案或者目錄名稱

2:引數說明:

-b : 只找二進位制檔案

-m: 只找在說明檔案manual路徑下的檔案

-s : 只找source原始檔

-u : 沒有說明文件的檔案

       3:例子:

              whereispasswd  將和passwd檔案相關的檔案都查找出來

四:which 可以找到可執行檔案和別名(alias)

       1:基本語法:which 可執行檔名稱

       2:例子:which passwd which是通過 PATH環境變數到該路徑內查詢可執行檔案,所以基本的功能是尋找可執行檔案