常用搜尋命令------whereis,which,locate,find,findwhereislocate

來源:互聯網
上載者:User

常用搜尋命令------whereis,which,locate,find,findwhereislocate

摘自:http://312788172.iteye.com/blog/730280

我們經常在linux要尋找某個檔案,但不知道放在哪裡了,可以使用下面的一些命令來搜尋。這些是從網上找到的資料,因為有時很長時間不會用到,當要用的時候經常弄混了,所以放到這裡方便使用。 
which       查看可執行檔的位置 
whereis    查看檔案的位置 
locate       配 合資料庫查看檔案位置 
find          實際搜尋硬碟查詢檔案名稱 

1、which 
文法: 
[root@redhat ~]# which 可執行檔名稱 
例如: 
[root@redhat ~]# which passwd 
/usr/bin/passwd 
which是通過 PATH環境變數到該路徑內尋找可執行檔,所以基本的功能是尋找可執行檔 

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

[root@redhat ~]# whereis -b passwd 
passwd: /usr/bin/passwd /etc/passwd 
只將二進位檔案 尋找出來 

和find相比,whereis尋找的速度非常快,這是因為linux系統會將 系統內的所有檔案都記錄在一個資料庫檔案中,當使用whereis和下面即將介紹的locate時,會從資料庫中尋找資料,而不是像find命令那樣,通 過遍曆硬碟來尋找,效率自然會很高。 
但是該資料庫檔案並不是即時更新,預設情況下時一星期更新一次,因此,我們在用whereis和locate 尋找檔案時,有時會找到已經被刪除的資料,或者剛剛建立檔案,卻無法尋找到,原因就是因為資料庫檔案沒有被更新,所以在使用前需要先更新資料庫檔案,命令為: updatedb

3、 locate 
文法: 
[root@redhat ~]# locate 檔案或者目錄名稱 
例 如: 
[root@redhat ~]# locate passwd 
/home/weblogic/bea/user_projects/domains/zhanggongzhe112/myserver/stage/_appsdir_DB_war/DB.war/jsp/as/user/passwd.jsp
/home/weblogic/bea/user_projects/domains/zhanggongzhe112/myserver/stage/_appsdir_admin_war/admin.war/jsp/platform/passwd.jsp
/lib/security/pam_unix_passwd.so 
/lib/security/pam_passwdqc.so 
/usr/include/rpcsvc/yppasswd.x 
/usr/include/rpcsvc/yppasswd.h 
/usr/lib/perl5/5.8.5/i386-linux-thread-multi/rpcsvc/yppasswd.ph 
/usr/lib/kde3/kded_kpasswdserver.la 
/usr/lib/kde3/kded_kpasswdserver.so 
/usr/lib/ruby/1.8/webrick/httpauth/htpasswd.rb 
/usr/bin/vncpasswd 
/usr/bin/userpasswd 
/usr/bin/yppasswd 
………… 

4、 find 
文法: 
[root@redhat ~]# find 路徑 參數 
參 數說明: 
時間尋找參數: 
-atime n :將n*24小時記憶體取過的的檔案列出來 
-ctime n :將n*24小時內改變、新增的檔案或者目錄列出來 
-mtime n :將n*24小時內修改過的檔案或者目錄列出來 
-newer file :把比file還要新的檔案列出來 
名稱尋找參數: 
-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是在硬碟上遍曆查 找,因此非常消耗硬碟的資源,而且效率也非常低,因此建議大家優先使用whereis和locate。 
locate 是在資料庫裡尋找,資料庫大至每天更新一次。 
whereis 可以找到可執行命令和man page 
find 就是根據條件尋找檔案。 
which 可以找到可執行檔和別名(alias)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.