161

What's the difference between which and whereis?

Giacomo1968
  • 58,727
mk12
  • 3,342

6 Answers6

180

How about learning about whereis and which using whatis?

$  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

Basically, whereis searches for "possibly useful" files, while which only searches for executables.

I rarely use whereis. On the other hand, which is very useful, specially in scripts. which is the answer for the following question: Where does this command come from?

$  which ls
/bin/ls

$  whereis ls
ls: /bin/ls /usr/share/man/man1p/ls.1p.bz2 /usr/share/man/man1/ls.1.bz2
30

whereis searches the standard *nix locations for a specified command.

which searches your user-specific PATH (which may include some of the locations whereis searches, and may not include others - it might also include some places that whereis doesn't search if you'd added to your PATH)

Amber
  • 611
7

Quoting their man pages :

whereis :

whereis locates source/binary and manuals sections for specified files.

For instance :

$ whereis php
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz

ie, the "php" executable, and some other stuff (like man pages).


and which :

which returns the pathnames of the files which would be executed in the current environment

For instance :

$ which php
/usr/bin/php

ie, only the "php" executable.

2

which search for executables in the directories specified by the environment variable PATH. And if found out, the full pathname of this executable will be printed.

$ which ls
/bin/ls
$ which ifconfig
$ # No output, because ifconfig only exist in root's PATH.

whereis search for executables, source files, and manual pages using a database built by system automatically.

$ whereis less
less: /bin/less /usr/bin/less /usr/bin/X11/less /usr/share/man/man1/less.1.gz

But it seems that whereis and locate don't use the same database. When I installed a software and then used whereis and locate immediately to search for this software. The result is that whereis could find out some files related to this software while locate couldn't. Do they really use different database? How the database work? --Well, how about refuse to be a pedant? :)

1

Thought I'd share something I learned recently on my Apple MacOS X Mojave; pretty sure it applies to numerous versions of Apple's macOS over the past few years:

On macOS:

  • whereis searches for executables in the path defined by the string user.cs_path; further defined as: /usr/bin:/bin:/usr/sbin:/sbin.

  • whereis also displays location of system documentation (i.e. man pages) in some cases.

  • On some systems, sysctl may be used to "set or get kernel state", but Apple has deprecated the -w, --write option for changing user.cs_path using sysctl; IOW Apple has removed the ability to correct the faulty information whereis outputs.

  • In effect then, Apple has relegated whereis to reporting the location of the (mostly) 10-20 year-old tools they provide to their customers in user.cs_path.

  • All of this may make Apple's whereis the most useless utility on the planet.

Just in case you're interested :)

Seamus
  • 204
0

Primarily finds the location of executable files in your system's PATH environment variable. It tells you which executable will be run when you type a command.