LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   What is the difference between whereis and which commands (https://www.linuxquestions.org/questions/linux-newbie-8/what-is-the-difference-between-whereis-and-which-commands-655031/)

elias_vc 07-11-2008 02:20 AM

What is the difference between whereis and which commands
 
Hello, I was wondering what was is the difference between the output of which and whereis when it comes to binaries. I mean when I type for example
whereis man I get something this (I'm using Ubuntu 8.04):
man: /usr/bin/man /usr/local/man /usr/share/man /usr/share/man/man7/man.7.gz /usr/share/man/man1/man.1.gz

If I type which man, I get this:
/usr/bin/man

If /usr/bin/man /usr/local/man /usr/share/man are supposed to be the binary files for the man command then why which only displays /usr/bin/man? I read the man pages but I couldn't figure it out.

a_m0d 07-11-2008 02:34 AM

whereis will show you the location of the binary, the source, and the man pages for the command, whereas which will only show you the location of the binary for the command (these are straight out of the man entries, btw)

Mr. C. 07-11-2008 02:36 AM

Or to add a little more, which uses your PATH to search for a executable.

jomen 07-11-2008 02:39 AM

One caveat for whereis:
Quote:

whereis has a hard-coded path, so may not always find what you’re looking for.

Vit77 07-11-2008 04:21 AM

whereis searches through a list of certain directories (listed in man) for source/binary and manuals sections.
which searches only for binaries in $PATH.

elias_vc 07-11-2008 01:13 PM

Jomen when you say "a hard coded path" what do you mean?

forrestt 07-11-2008 01:18 PM

Another note, which searches your aliases then your $PATH and gives you the FIRST executable it finds, whereis tells you everything it finds.

HTH

Forrest

Mr. C. 07-11-2008 01:29 PM

"hard coded path" in this context means the program only looks in a set of directories programmed into the program itself, such as:
Code:

man whereis:
...
FILES
/{bin,sbin,etc}

/usr/{lib,bin,old,new,local,games,include,etc,src,man,sbin,
X386,TeX,g++-include}

/usr/local/{X386,TeX,X11,include,lib,man,etc,bin,games,emacs}

BUGS
...
whereis has a hard-coded path, so may not always find what you're look-
ing for.

8 May 1994                        WHEREIS(1)

It will not look in your own bin, or any other bin directory not listed above.

Mr. C. 07-11-2008 01:32 PM

Quote:

Originally Posted by forrestt (Post 3211460)
Another note, which searches your aliases then your $PATH and gives you the FIRST executable it finds, whereis tells you everything it finds.

Well, sir, this all depends on which which you are using, and which which will indicate which which is in actual use which is exactly which fact one wishes to discover when wondering which which, and which which performs which behavior.

forrestt 07-11-2008 01:37 PM

I don't know if I'm supposed to laugh or say, "I haven't met a which which doesn't perform the way which I described."

Mr. C. 07-11-2008 01:41 PM

Quote:

Originally Posted by forrestt (Post 3211485)
I don't know if I'm supposed to laugh or say, "I haven't met a which which doesn't perform the way which I described."


....which means you haven't looked very hard:

Code:

$ alias hi
alias hi='history'
$ which hi
alias hi='history'
$ alias
$ /usr/bin/which hi
/usr/bin/which: no hi in (/home/mrc/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11R6/bin:/usr/sbin:/sbin:.:/home/mrc/bin)

Now you can cry.

forrestt 07-11-2008 01:47 PM

I'll not cry, just add that most of my career I've used tcsh with a built in which, so don't go around meeting strange whiches often.

Mr. C. 07-11-2008 02:05 PM

It is an important distinction.

Code that assumes builtin behavior is not portable,
and causes problems when script is invoked from sub-shells which for speed reasons, or
to control name space pollution, do not source shell startup files.

But more importantly, it confuses new users who are not aware of the built-in vs
external commands.

Furthermore, many users still use or require standard Bourne shell behavior, or even
ksh, which do not have built-ins (ksh has whence).

This commentary isn't directed at you - it is merely for those who are interested in
knowing about these distinctions.

forrestt 07-11-2008 02:14 PM

Also, the main point I was trying to make wasn't the fact that aliases are searched, but rathar that which will only show the FIRST thing it finds. This what what I initially thought you were disputing.

Mr. C. 07-11-2008 02:16 PM

And a excellent point it is.


All times are GMT -5. The time now is 08:33 AM.