linux常用查詢命令

2021-07-23 10:45:55 字數 2668 閱讀 1178

我們經常在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 查詢檔案時,有時會找到已經被刪除的資料,或者剛剛建立檔案,卻無法查詢到,原因就是因為資料庫檔案沒有被更新。

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)

Linux常用查詢命令

簡單快速方法 whereis ls可以搜尋到 ls 命令以及它的聯機幫助文件所在的位置。由於 whereis 是在乙個資料庫中 var lib mlocate 查詢,因此 whereis 查詢速度很快。這個資料庫是 linux 是系統自動建立的,包含有本地所有檔案資訊,並且每天通過自動執行 upda...

Linux常用查詢命令

find 語法 find 搜尋範圍 匹配條件 作用 查詢檔案或目錄 引數說明 name 按檔名稱查詢 user 按檔案擁有者查詢 size 根按檔案大小查詢檔案 n大於,n小於,n等於 舉例在test目錄下查詢test1.txt檔案 root ljh2020 find test name test1...

Linux常用查詢命令

find使用基本格式如下 find 指定目錄 指定條件 指定動作 以下是幾個示例,搜尋範圍包括指定目錄和子目錄 在根目錄下查詢檔案名以.txt結尾的檔案 find name txt 在根目錄下查詢檔案名以abc開頭的檔案,並顯示詳情 find name abc ls 在當前目錄中搜尋過去10分鐘更新...