技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

linux系統(tǒng)中whereis的用法詳解

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2018-05-23 00:32┆點擊:

linux/Unix可以使用 whereis 這個命令來查找某個特定的程序和命令的二進(jìn)制文件()、源代碼和man手冊的位置,貌似現(xiàn)在還能看到軟件的配置文件的位置(路徑)。

 


命令作用:

查找二進(jìn)制文件位置
查找軟件的配置文件的位置
查找源代碼文件位置
查看man手冊位置

語法

基本語法如下:

whereis command
OR
whereis program
OR
whereis [options] program
OR
whereis -BMS directory -f command
whereis 使用舉例:
查找 date 命令的 可執(zhí)行文件、源代碼和man手冊的位置,輸入:
$ whereis date
輸出如下圖:
whereis-command-demo.gif
Animated gif 01: whereis command running on my Debian based server
如何只搜索二進(jìn)制文件的位置?
使用 -b 參數(shù) :
$ whereis -b date
如何只搜索man手冊的位置?
使用 -m 參數(shù):
$ whereis -m date
如何只搜索源代碼文件的位置?
使用 -s 參數(shù):
$ whereis -s date
問題:whereis -u參數(shù)是有問題的,按照man whereis 的說明,-u的就是搜索那些沒有二進(jìn)制文件或者源代碼文件或者man手冊的文件的。但是實際測試發(fā)現(xiàn),和這毛關(guān)系都沒有啊。
man手冊上的一個例子:
A file is said to be unusual if it does not have one entry of each requested type. Thus the following example, asks for those files in the current directory which have no documentation(意思是搜索當(dāng)前目錄下,沒有man文檔的文件):
$ whereis -m -u *
我們先cd /bin ,然后執(zhí)行上面的命令,會發(fā)現(xiàn)  whereis -m -u *  和 where -m *   結(jié)果是一模一樣的。-u的功能完全沒體現(xiàn)出來。而且與man文檔描述的完全不符,因為/bin目錄下的文件都是有man文檔的,按man文檔的意思,結(jié)果應(yīng)該是空的,但是結(jié)果卻不是空的。
如何限制搜索的路徑?
使用下面的參數(shù)限制對應(yīng)的搜索路徑:
-B /path/to/dir : 限制在指定的目錄搜索二進(jìn)制文件.
-M /path/to/dir : 限制在指定的目錄搜索man手冊文件.
-S /path/to/dir : 限制在指定的目錄搜索二進(jìn)制文件.
在使用了-B , -M , -S  任意一個參數(shù)時,必須加上 -f  參數(shù), 然后指定要搜索的文件名.
實例如下:只在 /bin 目錄下搜索 ls 和gcc的:
$ whereis -B /bin -f ls gcc
結(jié)果如下:

1
2
ls: /bin/ls /usr/share/man/man1/ls.1.gz
gcc:
可以看到,gcc在/bin目錄下搜索二進(jìn)制文件是沒有結(jié)果的,說明gcc的二進(jìn)制文件不在 /bin目錄下。
問題:但是,我發(fā)現(xiàn),雖然/bin目錄下沒有g(shù)cc二進(jìn)制文件,但是使用上面的命令照樣有輸出,而不是像我翻譯的這篇文章那樣。
 
實例2:這也是man手冊上的例子,經(jīng)測試,這里 -u參數(shù)還是和man手冊上描述的不符合。
查找所有/usr/bin目錄下的,其man文檔不在 /usr/man/man1/  ,且其源代碼文件不在/usr/src/ 的 文件, 輸入:
# cd /usr/bin
# whereis -u -ms -M /usr/man/man1 -S /usr/src -f *
測試:
#cd  /bin
#whereis -u  -m -M /root  -f *
按man手冊的意思,這行命令的功能是:查找 所有/bin下,其man文檔不在/root的文件。所以應(yīng)該是有結(jié)果輸出的,因為/root目錄下根本沒有任何文件的man手冊??梢?,驚奇的發(fā)現(xiàn),結(jié)果居然是空的。
whereis command options
From the whereis(1) command man page:
Option Meaning
-f Define search scope.
-b Search only binaries.
-B Define binaries lookup path.
-m Search only manual paths.
-M Define man lookup path.
-s Search only sources path.
-S Define sources lookup path.
-u Search from unusual enties.
-V Output version information and exit.無效的,man文檔無此參數(shù)
-h Display this help and exit.    無效的,man文檔無此參數(shù)
SEE ALSO
whereis(1) Linux/Unix command man page
Category List of Unix and Linux commands
File Management cat
Network Utilities dig • host • ip
Processes Management bg • chroot • disown • fg • jobs • kill • killall • pwdx • time • pidof • pstree
Searching whereis • which
User Information groups • id • last • lastcomm • logname • users • w • who • whoami • lid • members
關(guān)于whereis 的 -u參數(shù)的功能,因為不知道whereis的版本,不好查找對應(yīng)版本的whereis的源代碼,我從網(wǎng)上找了個新版本的whereis的c源代碼,明顯的發(fā)現(xiàn),whereis使用hard-coded paths。
whereis在git上的代碼地址:https://github.com/karelzak/util-linux/blob/master/misc-utils/whereis.c#L96

代碼如下: