locate, which, and whereis - Three Ways to Find Commands in Linux

locate, which, and whereis - Three Ways to Find Commands in Linux

What You Will Learn

Lina: Linny-senpai, I installed a command but I have no idea where it went...
Linny-senpai: That happens all the time! Linux has three commands specifically for finding where things live: which, whereis, and locate. They each do something different — let's go through them one by one.

By the end of this article you will be able to:

  • Use which to find the executable path of a command
  • Use whereis to find the binary, man page, and source at once
  • Use locate to search for any file by name, fast
  • Know which tool to reach for in which situation

1. which — "Which executable does my shell actually run?"

Lina: When would I use which?
Linny-senpai: Say you have both Python 2 and Python 3 installed. If you just type python, which one runs? which tells you exactly which file your shell picks up.

Basic usage

which python3
/usr/bin/python3

which searches the directories listed in your PATH environment variable, in order, and prints the first match it finds.

which git
/usr/bin/git

which only searches directories in $PATH. Run echo $PATH to see which directories are included.

When a command is not found

which nonexistent-command

No output (exit code 1). Some distributions print a message like:

which: no nonexistent-command in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin)
Lina: Why is there no output?
Linny-senpai: It means the command isn't anywhere in your PATH — either it's not installed, or the installation directory isn't in PATH.

Useful when multiple versions are installed

which python
which python3
/usr/bin/python
/usr/bin/python3

You can see at a glance which version each name maps to.


2. whereis — "Show me the binary, man page, and source together"

Lina: I can get the path with which. What does whereis add?
Linny-senpai: which only shows the one executable your shell would run right now. whereis shows you the binary, the man page, and sometimes the source — all at once. And it's not limited to your current PATH.

Basic usage

whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

/usr/bin/ls is the executable. /usr/share/man/man1/ls.1.gz is the compressed man page.

whereis python3
python3: /usr/bin/python3 /usr/lib/python3 /usr/share/man/man1/python3.1.gz

What each field means

Field Content
First path after command: Executable binary
Path under /usr/share/man/ Man page
Path under /usr/lib/ Libraries or supporting files
Path under /usr/src/ Source code (if installed)

Filtering with options

whereis -b ls   # binary only
whereis -m ls   # man page only
whereis -s ls   # source only
ls: /usr/bin/ls
ls: /usr/share/man/man1/ls.1.gz
ls:

Unlike which, whereis searches a fixed set of standard system directories (/bin, /usr/bin, /usr/local/bin, etc.) regardless of your current PATH. That means it can still find a command even if PATH is misconfigured.


3. locate — "Search for any file by name, fast"

Lina: What is locate for?
Linny-senpai: It's not just for commands — you can find any file by name, really fast. Think of searching for a config file whose exact location you forgot. locate is much faster than find for that.

Basic usage

locate ssh_config
/etc/ssh/ssh_config
/usr/share/doc/openssh-client/examples/ssh_config

Why is it so fast?

locate does not scan the filesystem in real time. It queries a pre-built database (index), which is why it returns results almost instantly.

The database is typically rebuilt once a day by a scheduled job running updatedb. Files created after the last update will not appear in locate results until the database is refreshed.

Rebuild the database manually

sudo updatedb

Run this after creating new files, then locate will find them.

locate -i README
/home/user/projects/readme.md
/usr/share/doc/curl/README
/usr/share/doc/git/README.md

Combine with grep to filter results

locate nginx | grep conf
/etc/nginx/nginx.conf
/etc/nginx/conf.d/default.conf

Count matches without listing all paths

locate -c python
248

If locate is not installed, install it with sudo apt install mlocate (Debian/Ubuntu) or sudo dnf install mlocate (Fedora/RHEL).


4. Choosing the Right Command

Lina: Okay, can you give me a simple rule for which to use when?
Linny-senpai: Sure — here's the one-line version for each!
Goal Command
Find the exact executable your shell would run which
See the binary, man page, and source together whereis
Search for any file by name, quickly locate
Search for newly created files (not yet in DB) find

Common real-world examples

  • Is python Python 2 or Python 3? → which python
  • Where is git's man page? → whereis git
  • Where is nginx.conf? → locate nginx.conf
  • Just installed something and need to find it now → sudo updatedb && locate filename

5. Common Questions and Pitfalls

Q. which returns nothing — is the command really installed?

Lina: I ran which node and got nothing. I thought I installed it...
Linny-senpai: Try whereis node and locate node | grep bin first. Those cast a wider net. Also check echo $PATH — maybe the install directory just isn't in your PATH.
# Check PATH
echo $PATH

# Broader search
whereis node

# Search common bin directories
locate node | grep "/bin/"

Q. locate returns stale results

If a file you just created does not appear:

sudo updatedb
locate the-file-you-want

Q. locate command not found

# Debian / Ubuntu
sudo apt install mlocate

# Fedora / RHEL / CentOS
sudo dnf install mlocate

Next Reading