whereis 返回不同的路径

use*_*006 8 command-line bash

我使用命令 whereis rm

$ whereis rm
/bin/rm
Run Code Online (Sandbox Code Playgroud)

然后我试试 which rm

$ which rm
/usr/local/opt/coreutils/libexec/gnubin/rm
Run Code Online (Sandbox Code Playgroud)

结果是不同的......所以rm当我运行像这样的命令时会调用哪个rm readme

mat*_*ure 7

Denilson Sá Maia对超级用户回答可能有用:

which 搜索特定于用户的 PATH(可能包括 whereis 搜索的一些位置,可能不包括其他位置 - 它还可能包括一些 whereis如果您已添加到 PATH不搜索的位置)。

whereis 搜索指定命令的标准 *nix 位置。

$  whatis which
which                 (1)  - shows the full path of (shell) commands

$  whatis whereis
whereis              (1)  - locate the binary, source, and manual page files for a command
Run Code Online (Sandbox Code Playgroud)

基本上,whereis搜索“可能有用”的文件,而which只搜索可执行文件。

我很少用whereis。另一方面,which非常有用,特别是在脚本中。which是以下问题的答案:此命令从何而来?

$  which ls
/bin/ls

$  whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.bz2 /usr/share/man/man1/ls.1.bz2
Run Code Online (Sandbox Code Playgroud)