主頁 > 知識庫 > Linux下which、whereis、locate、find 區(qū)別

Linux下which、whereis、locate、find 區(qū)別

熱門標簽:電話機器人搭建 硅谷的囚徒呼叫中心 外呼系統(tǒng) 解決方案 服務器配置 家政服務網絡 百度競價點擊價格的計算公式 美團

我們經常在Linux要查找某個文件或命令,但不知道放在哪里了,可以使用下面的一些命令來搜索。

which      查看可執(zhí)行文件的位置
whereis    查看文件的位置
locate     配合數(shù)據庫查看文件位置
find       實際搜尋硬盤查詢文件名稱

1、which

語法: which 可執(zhí)行文件名稱
例如:
[root@redhat ~]# which passwd
/usr/bin/passwd
which是通過 PATH 環(huán)境變量到該路徑內查找可執(zhí)行文件,所以基本的功能是尋找可執(zhí)行文件

2、whereis

語法: whereis [-bmsu] 文件或者目錄名稱
參數(shù)說明:
-b : 只找二進制文件
-m: 只找在說明文件manual路徑下的文件
-s : 只找source源文件
-u : 沒有說明文檔的文件
例如:
[root@redhat ~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd /usr/share/man/man5/passwd.5.gz /usr/share/man/man1/passwd.1.gz /usr/share/man/man1/passwd.1ssl.gz
將和passwd文件相關的文件都查找出來

[root@redhat ~]# whereis -b passwd
passwd: /usr/bin/passwd /etc/passwd /usr/bin/X11/passwd
只將二進制文件 查找出來

和find相比,whereis查找的速度非常快,這是因為linux系統(tǒng)會將系統(tǒng)內的所有文件都記錄在一個數(shù)據庫文件中,當使用whereis和下面即將介紹的locate時,會從數(shù)據庫中查找數(shù)據,而不是像find命令那樣,通過遍歷硬盤來查找,效率自然會很高。
但是該數(shù)據庫文件并不是實時更新,默認情況下時一星期更新一次,因此,我們在用whereis和locate 查找文件時,有時會找到已經被刪除的數(shù)據,或者剛剛建立文件,卻無法查找到,原因就是因為數(shù)據庫文件沒有被更新。

3、 locate

語法: locate 文件或者目錄名稱
例 如:

[root@redhat ~]# locate passwd 
/etc/passwd
/etc/passwd-
/etc/cron.daily/passwd
/etc/init/passwd.conf
/etc/init.d/passwd
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswd
………… 


4、 find

語法: find 路徑 參數(shù)
參數(shù)說明:
時間查找參數(shù):
-atime n :將n*24小時內存取過的的文件列出來
-ctime n :將n*24小時內改變、新增的文件或者目錄列出來
-mtime n :將n*24小時內修改過的文件或者目錄列出來
-newer file :把比file還要新的文件列出來
名稱查找參數(shù):
-gid n       :尋找群組ID為n的文件
-group name  :尋找群組名稱為name的文件
-uid n       :尋找擁有者ID為n的文件
-user name   :尋找用戶者名稱為name的文件
-name file   :尋找文件名為file的文件(可以使用通配符)
例如:

[root@redhat ~]# find / -name zgz 
/home/zgz 
/home/zgz/zgz 
/home/weblogic/bea/user_projects/domains/zgz 
/home/Oracle/product/10g/cfgtoollogs/dbca/zgz 
/home/oracle/product/10g/cfgtoollogs/emca/zgz 
/home/oracle/oradata/zgz 
[root@redhat ~]# find / -name '*zgz*' 
/home/zgz 
/home/zgz/zgz1 
/home/zgz/zgzdirzgz 
/home/zgz/zgz 
/home/zgz/zgzdir 
/home/weblogic/bea/user_projects/domains/zgz 
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00006 
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00002 
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00004 
/home/weblogic/bea/user_projects/domains/zgz/zgz.log 
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00008 
/home/weblogic/bea/user_projects/domains/zgz/zgz.log00005 

當我們用whereis和locate無法查找到我們需要的文件時,可以使用find,但是find是在硬盤上遍歷查找,因此非常消耗硬盤的資源,而且效率也非常低,因此建議大家優(yōu)先使用whereis和locate。

總結:

which     只能查可執(zhí)行文件和別名(alias) ,并在PATH變量里面尋找
whereis   只能查二進制文件(含可執(zhí)行文件)、說明文檔,源文件等,從linux文件數(shù)據庫(/var/lib/slocate/slocate.db 或 /var/lib/mlocate/mlocate.db)尋找,所以有可能找到剛剛刪除,或者沒有發(fā)現(xiàn)新建的文件
locate    在數(shù)據庫里查找,數(shù)據庫大至每天更新一次,文件名是部分匹配(見 3 locate passwd 的結果:opasswd)
find      最強大,什么都能查,根據條件查找文件,在硬盤上查找,效率很低

標簽:防城港 臨沂 南昌 韶關 烏蘭察布 撫州 邢臺 北海

巨人網絡通訊聲明:本文標題《Linux下which、whereis、locate、find 區(qū)別》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266