whereis 与 which 的区别

我们在使用 Linux 或者 macOS 时,需要查找一条命令的绝对路径,我们可能会使用 whereis 或者 which,今天我们就来比较一下两者的区别。

先来看看 whereis 的帮助文档:

The whereis utility checks the standard binary, and manual page directories for the specified programs, printing out the paths of any it finds.

whereis 会返回特定程序的二进制文件和帮助手册的位置。

下面是 which 的帮助文档:

The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.

which 会输出特定命令的可执行程序的完整路径。

which 查找一条命令是根据当前 Linux 的环境变量来查找的。被查找的命令在环境变量配置的对应目录下,则该命令可以被查找到并返回该命令的绝对路径;倘若环境变量配置的所有目录下没有找到对应的命令,则无输出。

whereis 可以通过参数搜索指定的目录。

whereis 和 which 返回的内容对比,whereis 还可以返回帮助文档的绝对路径

简单来说,都可以搜索可执行文件的绝对路径,whereis 可以指定搜索的路径和帮助文档,可用的参数也比 which 稍微多一点,而 which 支持查找环境变量里的目录,也只能搜索可执行文件的目录,没有返回帮助文档。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>