What is the difference between locate and which in Linux?

Why when I use locate mentor does it list all the mentor but when I used which mentor it says "no mentor in..." - what does this mean?

  • See this related unix.stackexchange.com/a/60208/84427 – pun Mar 29 '16 at 9:48
  • There are also where and whereis. – Jan Tojnar Mar 29 '16 at 20:20
  • in bash, prefer : type -all acommand . And If I read the XYProblem behind your question : you have a command, you can see it via locate, but can't execute it ? then edit PATH, either in your specific user's ~/.profile file, or if you are convinced you know what to do, in the global /etc/profile file [WARNING: you may break system scripts (and possibly a lot of the OS) if you add the wrong path in the $PATH of /etc/profile... (ex: paths containing commands usually found elswhere). Prefer only adding at the END of the variable. And please do not put :: or a . anywhere in that PATH. – Olivier Dulac Mar 30 '16 at 14:52
  • Possible duplicate of What is the difference between 'locate' and 'find' in Linux? – Abraxas Mar 30 '16 at 19:10

What is the difference between locate and which?

locate uses a previously built database to locate the file.

locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line.

Source locate(1) - Linux man page

updatedb creates or updates a database used by locate(1). If the database already exists, its data is reused to avoid rereading directories that have not changed.

updatedb is usually run daily by cron(8) to update the default database (/var/lib/mlocate/mlocate.db)

Source updatedb(8) - Linux man page

which looks for an executable file by searching for it in the directories in the PATH environmental variable.

which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH.

using the same algorithm as bash(1).

Source which(1) - Linux man page

locate mentor lists mentor, but which mentor says "no mentor in..."

What does that mean?

You have some files named mentor which can be found in the locate database.

You don't have an executable file or script named mentor in your PATH.

  • 2
    "updatedb" - good to know it's not just Microsoft that comes up with overly non-specific names for things. (In Win32, DeleteObject deletes a GDI object, CloseHandle closes a kernel handle, etc. Similarly updatedb updates the locate DB) – user253751 Mar 30 '16 at 2:39
  • "which" is also specifically useful in the event that you may have multiple versions of the same program kicking about in various places in your path, so it tells you which one is actually executed when you run the command. This has been helpful for me before in diagnosing unexpected behaviour - for instance, it helped me realise I had left a manually installed version of a utility on my system such that it was overriding the version installed by the package manager. – Carcer Mar 30 '16 at 9:39

which is to locate a command (which returns a path name of the files / links that would be executed in the current environment)

locate is to find files by name (locate reads one or more databases prepared by updatedb and writes files names matching at least one of the patterns to standered output, one per line)

  • 2
    Please provide attribution for your sources — the descriptions in your answer are taken directly from the man pages for those utilities. – Reinstate Monica iamnotmaynard Mar 29 '16 at 15:53

locate takes a glob pattern by default (Regex pattern can be used too) and searches the database /var/lib/mlocate/mlocate.db for any filename matching the pattern.

which is a command to search for the full path of a command in the directories interpreted by the PATH environment variable.

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.