为初学者介绍 Linux whereis 命令（5个例子）
whereis find
![](/data/attachment/album/201802/25/132156q2n3h3be03tn33ax.jpg)


有时，在使用命令行的时候，我们需要快速找到某一个命令的二进制文件所在位置。这种情况下可以选择 [find](https://www.howtoforge.com/tutorial/linux-find-command/) 命令，但使用它会耗费时间，可能也会出现意料之外的情况。有一个专门为这种情况设计的命令：`whereis`。


在这篇文章里，我们会通过一些便于理解的例子来解释这一命令的基础内容。但在这之前，值得说明的一点是，下面出现的所有例子都在 Ubuntu 16.04 LTS 下测试过。


### Linux whereis 命令


`whereis` 命令可以帮助用户寻找某一命令的二进制文件，源码以及帮助页面。下面是它的格式：



```
whereis [options] [-BMS directory... -f] name...

```

这是这一命令的 man 页面给出的解释：



> 
> `whereis` 可以查找指定命令的二进制文件、源文件和帮助文件。 被找到的文件在显示时，会去掉主路径名，然后再去掉文件的（单个）尾部扩展名 (如: `.c`)，来源于源代码控制的 `s.` 前缀也会被去掉。接下来，`whereis` 会尝试在标准的 Linux 位置里寻找具体程序，也会在由 `$PATH` 和 `$MANPATH` 指定的路径中寻找。
> 
> 
> 


下面这些以 Q&A 形式出现的例子，可以给你一个关于如何使用 whereis 命令的直观感受。


### Q1. 如何用 whereis 命令寻找二进制文件所在位置?


假设你想找，比如说，`whereis` 命令自己所在位置。下面是你具体的操作：



```
whereis whereis

```

[![How to find location of binary file using whereis](/data/attachment/album/201802/25/132209hea1ryrzpw11faga.png)](https://www.howtoforge.com/images/command-tutorial/big/whereis-basic-usage.png)


需要注意的是，输出的第一个路径才是你想要的结果。使用 `whereis` 命令，同时也会显示帮助页面和源码所在路径。（如果能找到的情况下会显示，但是在这一例中没有找到）所以你在输出中看见的第二个路径就是帮助页面文件所在位置。


### Q2. 怎么在搜索时规定只搜索二进制文件、帮助页面，还是源代码呢？


如果你想只搜索，假设说，二进制文件，你可以使用 `-b` 这一命令行选项。例如：



```
whereis -b cp

```

[![How to specifically search for binaries, manuals, or source code](/data/attachment/album/201802/25/132210x16gujmn1s8n0nzn.png)](https://www.howtoforge.com/images/command-tutorial/big/whereis-b-option.png)


类似的， `-m` 和 `-s` 这两个 选项分别对应 帮助页面和源码。


### Q3.如何限制 whereis 命令搜索位置？


默认情况下，`whereis` 是从由匹配符所定义的硬编码路径来寻找文件的。但如果你想的话，你可以用命令行选项来限制搜索。例如，如果你只想在 `/usr/bin` 寻找二进制文件，你可以用 `-B` 这一选项来实现。



```
whereis -B /usr/bin/ -f cp

```

注意：使用这种方式时可以给出多个路径。使用 `-f` 这一选项来明确分隔目录列表和要搜索的文件名。


类似的，如果你想只搜索帮助文件或源码，你可以对应使用 `-M` 和 `-S` 这两个选项。


### Q4. 如何查看 whereis 的搜索路径?


与此相对应的也有一个选项。只要在 `whereis` 后加上 `-l`。



```
whereis -l

```

这是例子的部分输出结果：


[![How to see paths that whereis uses for search](/data/attachment/album/201802/25/132210kz0392d1fl1ndzgf.png)](https://www.howtoforge.com/images/command-tutorial/big/whereis-l.png)


### Q5. 如何找到一个有异常条目的命令?


对于 `whereis` 命令来说，如果一个命令对每个显式的请求类型都不止一项，则该命令被视为异常。例如，没有可用文档的命令，或者对应文档分散在各处的命令都可以算作异常命令。 当使用 `-u` 这一选项，`whereis` 就会显示那些有异常条目的命令。


例如，下面这一例子就显示，在当前目录中，没有对应文档或有多个文档的命令。



```
whereis -m -u *

```

### 总结


我觉得，`whereis` 不是那种你需要经常使用的命令行工具。但在遇到某些特殊情况时，它绝对会让你的生活变得轻松。我们已经涉及了这一工具提供的一些重要命令行选项，所以要注意练习。想了解更多信息，直接去看它的 [man](https://linux.die.net/man/1/whereis) 页面吧。




---


via: <https://www.howtoforge.com/linux-whereis-command/>


作者：[Himanshu Arora](https://www.howtoforge.com) 译者：[wenwensnow](https://github.com/wenwensnow) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Linux whereis Command Explained for Beginners (5 Examples)

### On this page

[Linux whereis command](#linux-whereis-command)[Q1. How to find location of binary file using whereis?](#q-how-to-find-location-of-binary-file-using-whereis)[Q2. How to specifically search for binaries, manuals, or source code?](#q-how-to-specifically-search-for-binaries-manuals-or-source-code)[Q3. How to limit whereis search as per requirement?](#q-how-to-limit-whereis-search-as-per-requirement)[Q4. How to see paths that whereis uses for search?](#q-how-to-see-paths-that-whereis-uses-for-search)[Q5. How to find command names with unusual entries?](#q-how-to-find-command-names-with-unusual-entries)[Conclusion](#conclusion)

Sometimes, while working on the command line, we just need to quickly find out the location of the binary file for a command. Yes, the [find](https://www.howtoforge.com/tutorial/linux-find-command/) command is an option in this case, but it's a bit time consuming and will likely produce some non-desired results as well. There's a specific command that's designed for this purpose: **whereis**.

In this article, we will discuss the basics of this command using some easy to understand examples. But before we do that, it's worth mentioning that all examples in this tutorial have been tested on Ubuntu 16.04LTS.

## Linux whereis command

The whereis command lets users locate binary, source, and manual page files for a command. Following is its syntax:

whereis [options] [-BMS directory... -f] name...

And here's how the tool's man page explains it:

whereis locates the binary, source and manual files for the specified command names. The supplied

names are first stripped of leading pathname components and any (single) trailing extension of the

form .ext (for example: .c) Prefixes of s. resulting from use of source code control are also dealt

with. whereis then attempts to locate the desired program in the standard Linux places, and in the

places specified by $PATH and $MANPATH.

The following Q&A-styled examples should give you a good idea on how the whereis command works.

## Q1. How to find location of binary file using whereis?

Suppose you want to find the location for, let's say, the whereis command itself. Then here's how you can do that:

whereis whereis

Note that the first path in the output is what you are looking for. The whereis command also produces paths for manual pages and source code (if available, which isn't in this case). So the second path you see in the output above is the path to the whereis manual file(s).

## Q2. How to specifically search for binaries, manuals, or source code?

If you want to search specifically for, say binary, then you can use the **-b** command line option. For example:

whereis -b cp

Similarly, the **-m** and **-s** options are used in case you want to find manuals and sources.

## Q3. How to limit whereis search as per requirement?

By default whereis tries to find files from hard-coded paths, which are defined with glob patterns. However, if you want, you can limit the search using specific command line options. For example, if you want whereis to only search for binary files in /usr/bin, then you can do this using the **-B** command line option.

whereis -B /usr/bin/ -f cp

**Note**: Since you can pass multiple paths this way, the **-f** command line option terminates the directory list and signals the start of file names.

Similarly, if you want to limit manual or source searches, you can use the **-M** and **-S** command line options.

## Q4. How to see paths that whereis uses for search?

There's an option for this as well. Just run the command with **-l**.

whereis -l

Here is the list (partial) it produced for us:

## Q5. How to find command names with unusual entries?

For whereis, a command becomes unusual if it does not have just one entry of each explicitly requested type. For example, commands with no documentation available, or those with documentation in multiple places are considered unusual. The **-u** command line option, when used, makes whereis show the command names that have unusual entries.

For example, the following command should display files in the current directory which have no documentation file, or more than one.

whereis -m -u *

## Conclusion

Agreed, whereis is not the kind of command line tool that you'll require very frequently. But when the situation arises, it definitely makes your life easy. We've covered some of the important command line options the tool offers, so do practice them. For more info, head to its [man page](https://linux.die.net/man/1/whereis).