What's the difference between which and whereis ?
5 Answers
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
- 13,151
-
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 forwhich. – Jordan Reiter Aug 21 '16 at 22:25 -
1@JordanReiter: It can't be!
whichshows the actual path. Are you sure the path pointed bywhichisn't just a symlink to the path pointed bywhereis? Maybe it is a shell alias. In bash, try runningtype your_cmd_here. – Denilson Sá Maia Aug 22 '16 at 00:01 -
@DenilsonSá unfortunately I can't recreate the situation but when I run into it again I'll provide more details. – Jordan Reiter Sep 01 '16 at 18:14
-
@DenilsonSáMaia, I get the same thing.
– SO_fix_the_vote_sorting_bug May 30 '18 at 21:32xcodebuild 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. -
I have mutliple python installations. Some in
/usr/bin/python2.7, some in/usr/local/lib/python3.4.whereis pythonfinds them both which is a great way to list all python versions installed – lucidbrot Aug 24 '19 at 13:45 -
-
On macOS,
whereissearches only for programs and not man pages or sources; it does this in "standard binary directories"; whilewhichsearches for programs in currentPATH. Of course, macOS isn't *nix strictly. – legends2k Dec 22 '20 at 16:12
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)
- 601
-
3
-
Nope. Just a fairly common convention of creative wildcard use to refer to a family of similar operating systems. ;) – Sep 12 '09 at 22:28
-
Yes! For a good example, compare:
PATH='' /usr/bin/which vimvsPATH='' /usr/bin/whereis vim. Thewhereiscommand still locates the executable, even if your PATH is empty. – edan Jun 19 '20 at 18:35
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.
- 822
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? :)
- 21
-
did you run updatedb command? locate relies on that as far as I remember – Oliver M Grech Feb 02 '18 at 10:37
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 Mojave:
whereissearches for executables only in the path defined by the stringuser.cs_path; further defined as:/usr/bin:/bin:/usr/sbin:/sbin.whereisdisplays no information on any system documentation (i.e.manpages).On some systems,
sysctlmay be used to "set or get kernel state", but Apple has deprecated the-w, --writeoption for changinguser.cs_pathusingsysctl.In effect, Apple has relegated
whereisto reporting the location of the 10-20 year-old tools they provide to their customers inuser.cs_path.All of this may make Apple's
whereisthe most useless utility on the planet.
Just in case you're interested :)
- 173
Can anybody confirm this? Does Snow Leopard behave the same?
– Wolf Sep 12 '09 at 23:11typeis superior. It also knows about defined aliases, functions etc – phil294 Aug 19 '18 at 03:32