LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-11-2008, 03:20 AM   #1
elias_vc
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Rep: Reputation: 0
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.
 
Old 07-11-2008, 03:34 AM   #2
a_m0d
Member
 
Registered: Apr 2007
Location: Perth, WA, Australia
Distribution: Fedora 8, Fedora 10
Posts: 49

Rep: Reputation: 15
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)
 
Old 07-11-2008, 03:36 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Or to add a little more, which uses your PATH to search for a executable.
 
Old 07-11-2008, 03:39 AM   #4
jomen
Senior Member
 
Registered: May 2004
Location: Leipzig/Germany
Distribution: Arch
Posts: 1,687

Rep: Reputation: 54
One caveat for whereis:
Quote:
whereis has a hard-coded path, so may not always find what you’re looking for.
 
Old 07-11-2008, 05:21 AM   #5
Vit77
Member
 
Registered: Jun 2008
Location: Toronto, Canada
Distribution: SuSE, RHEL, Mageia
Posts: 132

Rep: Reputation: 17
whereis searches through a list of certain directories (listed in man) for source/binary and manuals sections.
which searches only for binaries in $PATH.
 
Old 07-11-2008, 02:13 PM   #6
elias_vc
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Original Poster
Rep: Reputation: 0
Jomen when you say "a hard coded path" what do you mean?
 
Old 07-11-2008, 02:18 PM   #7
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 07-11-2008, 02:29 PM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
"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.
 
Old 07-11-2008, 02:32 PM   #9
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Quote:
Originally Posted by forrestt View Post
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.
 
Old 07-11-2008, 02:37 PM   #10
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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."
 
Old 07-11-2008, 02:41 PM   #11
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
Quote:
Originally Posted by forrestt View Post
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.
 
Old 07-11-2008, 02:47 PM   #12
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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.
 
Old 07-11-2008, 03:05 PM   #13
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
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.
 
Old 07-11-2008, 03:14 PM   #14
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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.
 
Old 07-11-2008, 03:16 PM   #15
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
And a excellent point it is.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference of the commands ust Linux - Newbie 2 04-29-2008 03:27 AM
what is the difference between these commands ls * and echo * izrafel Linux - General 5 06-12-2007 01:49 PM
what's the difference between the two commands? edenCC Linux - General 4 01-05-2007 10:34 AM
what's difference between commands man and info? xcore_on Linux - Newbie 2 05-23-2006 10:18 AM
I can't see the difference between the two commands xailer Linux - Newbie 3 11-21-2003 03:14 PM


All times are GMT -5. The time now is 05:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration