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
/usr/bin/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; }'
/usr/bin/yacc

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…