which/whereis differences

119

29

What's the difference between which and whereis ?

mk12

Posted 2009-09-12T20:12:30.167

Reputation: 2 432

1@mk12 I feel like type is superior. It also knows about defined aliases, functions etc – phil294 – 2018-08-19T03:32:54.843

I'm sorry but on my 10.5.8 OS X system which and whereis always give the same results. Maybe this is a very OS X specific question, since I agree, a different result is expected. Maybe whereis does not comply to what it should do. At least the two examples (see below) are not working: whereis ls and whereis php always give the same result as which ...

Can anybody confirm this? Does Snow Leopard behave the same? – Wolf – 2009-09-12T23:11:42.960

1Yeah I know, thats why I asked this question. – mk12 – 2009-09-12T23:25:39.267

Answers

129

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

Denilson Sá Maia

Posted 2009-09-12T20:12:30.167

Reputation: 9 603

1There's more to it than that. On my system, whereis and which return different executable paths. I can only get the path to the one that actually runs with whereis, not the one for which. – Jordan Reiter – 2016-08-21T22:25:05.577

1@JordanReiter: It can't be! which shows the actual path. Are you sure the path pointed by which isn't just a symlink to the path pointed by whereis? Maybe it is a shell alias. In bash, try running type your_cmd_here. – Denilson Sá Maia – 2016-08-22T00:01:53.343

@DenilsonSá unfortunately I can't recreate the situation but when I run into it again I'll provide more details. – Jordan Reiter – 2016-09-01T18:14:34.200

@DenilsonSáMaia, I get the same thing.

xcodebuild is hashed (/usr/local/bin/xcodebuild)
$ which xcodebuild -->
/usr/local/bin/xcodebuild
$ whereis xcodebuild -->
/usr/bin/xcodebuild```

And running `xcodebuild` always picks the wrong one (i.e., the `/usr/bin` command) even though `/usr/local/bin` has higher `$PATH` priority.
 – jdk1.0  – 2018-05-30T21:32:57.953

I have mutliple python installations. Some in /usr/bin/python2.7, some in /usr/local/lib/python3.4. whereis python finds them both which is a great way to list all python versions installed – lucidbrot – 2019-08-24T13:45:39.630

35didn't know about whatis, thanks. – mk12 – 2009-09-12T20:39:26.817

1BTW, I'd remove the "osx" tag, as this question applies to all unix variants (including Linux, BSD, Mac OS X, ...) – Denilson Sá Maia – 2009-09-12T20:44:17.913

changed to unix – mk12 – 2009-09-12T23:24:22.560

24

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

Posted 2009-09-12T20:12:30.167

Reputation: 551

What is *nix? – mk12 – 2009-09-12T20:19:45.580

2Unix, Linux etc. (Mac OS X belonging in the etc.) – None – 2009-09-12T20:23:48.620

8Ohhh, haha, I always thought that stack overflow was censoring the U in unix whenever I saw that for some reason.. – mk12 – 2009-09-12T22:19:44.020

Nope. Just a fairly common convention of creative wildcard use to refer to a family of similar operating systems. ;) – None – 2009-09-12T22:28:30.837

6

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.

Pascal MARTIN

Posted 2009-09-12T20:12:30.167

Reputation: 772

1

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? :)

user192505

Posted 2009-09-12T20:12:30.167

Reputation: 11

did you run updatedb command? locate relies on that as far as I remember – Oliver M Grech – 2018-02-02T10:37:27.547