天天看點

檔案查找指令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環境變量到該路徑内查找可執行檔案,是以基本的功能是尋找可執行檔案

繼續閱讀