萬盛學電腦網

 萬盛學電腦網 >> 服務器教程 >> linux系統中whereis的用法詳解

linux系統中whereis的用法詳解

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

 


命令作用:

查找二進制文件位置
查找軟件的配置文件的位置
查找源代碼文件位置
查看man手冊位置

語法

基本語法如下:

whereis command
OR
whereis program
OR
whereis [options] program
OR
whereis -BMS directory -f command
whereis 使用舉例:
查找 date 命令的 可執行文件、源代碼和man手冊的位置,輸入:
$ whereis date
輸出如下圖:
whereis-command-demo.gif
Animated gif 01: whereis command running on my Debian based server
如何只搜索二進制文件的位置?
使用 -b 參數 :
$ whereis -b date
如何只搜索man手冊的位置?
使用 -m 參數:
$ whereis -m date
如何只搜索源代碼文件的位置?
使用 -s 參數:
$ whereis -s date
問題:whereis -u參數是有問題的,按照man whereis 的說明,-u的就是搜索那些沒有二進制文件或者源代碼文件或者man手冊的文件的。但是實際測試發現,和這毛關系都沒有啊。
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(意思是搜索當前目錄下,沒有man文檔的文件):
$ whereis -m -u *
我們先cd /bin ,然後執行上面的命令,會發現  whereis -m -u *  和 where -m *   結果是一模一樣的。-u的功能完全沒體現出來。而且與man文檔描述的完全不符,因為/bin目錄下的文件都是有man文檔的,按man文檔的意思,結果應該是空的,但是結果卻不是空的。
如何限制搜索的路徑?
使用下面的參數限制對應的搜索路徑:
-B /path/to/dir : 限制在指定的目錄搜索二進制文件.
-M /path/to/dir : 限制在指定的目錄搜索man手冊文件.
-S /path/to/dir : 限制在指定的目錄搜索二進制文件.
在使用了-B , -M , -S  任意一個參數時,必須加上 -f  參數, 然後指定要搜索的文件名.
實例如下:只在 /bin 目錄下搜索 ls 和gcc的:
$ whereis -B /bin -f ls gcc
結果如下:

1
2
ls: /bin/ls /usr/share/man/man1/ls.1.gz
gcc:
可以看到,gcc在/bin目錄下搜索二進制文件是沒有結果的,說明gcc的二進制文件不在 /bin目錄下。
問題:但是,我發現,雖然/bin目錄下沒有gcc二進制文件,但是使用上面的命令照樣有輸出,而不是像我翻譯的這篇文章那樣。
 
實例2:這也是man手冊上的例子,經測試,這裡 -u參數還是和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的文件。所以應該是有結果輸出的,因為/root目錄下根本沒有任何文件的man手冊。可以,驚奇的發現,結果居然是空的。
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文檔無此參數
-h Display this help and exit.    無效的,man文檔無此參數
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
關於whereis 的 -u參數的功能,因為不知道whereis的版本,不好查找對應版本的whereis的源代碼,我從網上找了個新版本的whereis的c源代碼,明顯的發現,whereis使用hard-coded paths。
whereis在git上的代碼地址:https://github.com/karelzak/util-linux/blob/master/misc-utils/whereis.c#L96

代碼如下:


/*-
 * Copyright (c) 1980 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 * This product includes software developed by the University of
 * California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * 1999-02-22 Arkadiusz Miśkiewicz <[email protected]>
 * - added Native Language Support
 * 2011-08-12 Davidlohr Bueso <[email protected]>
 * - added $PATH lookup
 *
 * Copyright (C) 2013 Karel Zak <[email protected]>
 *               2013 Sami Kerola <[email protected]>
 */
 
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
 
#include "xalloc.h"
#include "nls.h"
#include "c.h"
#include "closestream.h"
#include "canonicalize.h"
 
#include "debug.h"
 
UL_DEBUG_DEFINE_MASK(whereis);
UL_DEBUG_DEFINE_MASKNAMES(whereis) = UL_DEBUG_EMPTY_MASKNAMES;
 
#define WHEREIS_DEBUG_INIT (1 <<

copyright © 萬盛學電腦網 all rights reserved