13

What is the basic difference between locate whereis and which command.

The basic difference that I observed is that locate locates all the related file names in the entire filesystem, whereas whereis and which commands only give the location (system/local address of file) of installed application. How accurate is my observation?

How are these commands implemented internally. Why does locate work so fast as compared to the dash search, while locate has to search a particular filename matching the target string in the entire filesystem hierarchy?

  • 1
    and what about type ? :P – jamesmstone Jul 18 '16 at 1:11
  • @jamesmstone what do you mean by type, please elaborate – Chinmaya B Jul 18 '16 at 4:06
  • 1
    Sorry I was just attempting to add onto your question. type is another command that seems (to my eyes) do a similar thing to the ones you mentioned. – jamesmstone Jul 18 '16 at 4:55
  • @jamesmstone I dont think there is any type command which works in my system. I am unable to find any manual or info page for the type command. I tried man type and info type. – Chinmaya B Jul 21 '16 at 10:55
  • Hmm I wonder why... Are you using bash ? – jamesmstone Jul 21 '16 at 10:59
17

which finds the binary executable of the program (if it is in your PATH). man which explains more clearly:

which returns the pathnames of the files (or links) which would be executed in the current environment, had its arguments been given as commands in a strictly POSIX-conformant shell. It does this by searching the PATH for executable files matching the names of the arguments. It does not follow symbolic links.

whereis finds the binary, the source, and the man page files for a program. For example

$ whereis gimp
/usr/bin/gimp /usr/lib/gimp /etc/gimp /usr/share/gimp /usr/share/man/man1/gimp.1.gz

You can get extra detail by passing the output of these commands as arguments to ls -l or file

$ ls -l $(which gimp)
lrwxrwxrwx 1 root root 8 Jun 30 19:59 /usr/bin/gimp -> gimp-2.8

$ file $(which gimp)
/usr/bin/gimp: symbolic link to gimp-2.8

locate indeed finds all the files that have the pattern specified anywhere in their paths. You can tell it to only find files and directories whose names (rather than full paths) include the pattern with the -b option, which is usually what you want, and gives a less unwieldy list.

locate is fast because it uses a binary database that gets periodically updated (once daily, by cron). You can update it yourself to ensure recently added files are found by running sudo updatedb

One more thing about locate - it doesn't care whether files still exist or not, so to avoid finding recently deleted files, use -e. Often I also pipe to less as the list can be long. Typically I do:

sudo updatedb && locate -b -e gimp | less

How Unity's dash works is explained here - it uses Zeitgeist to index system files and learn from usage patterns, and enables other applications to make use of this data, so it is doing a lot more work than locate.

  • can you compare dash and locate command performance and implementation details? The updation of the binary database should be done after creation of any new file in system isn't it so that period would be indefinite correct or is it managed by system processes? – Chinmaya B Jul 17 '16 at 16:10
  • 2
    @Creator updating the database automatically is done by cron, once per day. You can check when you install something you can't immediately find the files with locate. The dash uses a completely different index - slower because it does more stuff (I edited my answer) – Zanna Jul 17 '16 at 16:34
  • 1
    So to sum up locate is useful and more powerful than which or whereis but is dumb while dash is more powerful and implements machine learning. and thanks for the sudo updatedb your command is very helpful didnt know that although I used -e regularly. – Chinmaya B Jul 17 '16 at 20:14
  • @Creator yep you phrased it better :) happy that it helped – Zanna Jul 17 '16 at 20:36
0

Simply

$ which samba

which command searches the list of programs listed down through the PATH settings

$ whereis samba

whereis command also searches for programs that are not present in the PATH setting

$locate samba

Also finds files by filename but does not search the directory structure itself but only a database prepared by updatedb. Because of that locate is faster than find but less accurate. Examples:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.