Linux: which vs. whereis

“which” is a very useful command used to find the location of an executable which is in your path e.g.:

# which yacc

I actually didn’t know that there is also a command “whereis” which is kind of similar:

 # whereis yacc
yacc: /usr/bin/yacc /usr/share/man/man1p/yacc.1p.gz

It doesn’t only return the location of the executable but also of source files and man pages. Another difference is also that it doesn’t search the configured path but looks through an internal list of common Unix paths.

So this basically means that there are executables which will be found by “which” but not by “whereis” because they are in a non-standard directory which is in the path. On the other hand, “whereis” will find other executables not found by which if some of the standard directories are not in the path.

So if you do not find an executable with one of the two commands, you should give the other one a try.

“which” stays my preferred command for this (and I’ll only use “whereis” if which fails to find something) because it’s just returning one full path without anything else which makes it so easy to combine it with other commands e.g.:

# cat `which yacc`
#! /bin/sh
exec /usr/bin/bison -y "$@"

Of course you can also tell “whereis” to only show binaries and then use awk to get the output you need:

# whereis -b yacc | awk '{ print $2; }'

So combining it with cat:

# cat `whereis -b yacc | awk '{ print $2; }'`
#! /bin/sh
exec /usr/bin/bison -y "$@"

But it’s of course much longer and more complex…

One thought on “Linux: which vs. whereis

  1. > it doesn’t search the configured path

    This is no longer true for more recent versions of the `whereis` utility. The version from the standard `util-linux` package does indeed search configured PATH. This change was added in version v2.21 of util-linux (in 2011):

    The mangpage now states: “whereis then attempts to locate the desired program in the standard Linux places, and in the places specified by $PATH”.

    Note: this only applies to Linux… AFAIK the current BSD version of `whereis` still doesn’t search PATH.

Leave a Reply

Your email address will not be published.