which vs whereis vs locate: Find Where a Command Lives

which vs whereis vs locate: Find Where a Command Lives

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:


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

Conclusion: which searches PATH in order and prints the one executable your shellAn interactive program that reads the commands you type and runs them. runs.

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 variableA named value that the shell or a program can look up while running., 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 directoryA container that organizes files. Same idea as a "folder" on Windows or macOS. 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.

Check aliases and shell builtins too: type -a / command -v

Lina: I ran which cd on Ubuntu and got nothing. But cd always works...
Linny-senpai: cd is built into the shell (bash) itself. On Ubuntu and Debian, which only looks for files in your PATH, so it can't see shell builtins or aliases — it prints nothing and returns exit code 1. For those, use type -a.

Fedora and RHEL-family systems behave differently. The bash package installs wrapper scripts such as /usr/bin/cd for the builtins, so which cd prints that path, and in interactive shells /etc/profile.d/which2.sh makes which show aliases and shell functions as well. Whatever the system, type -a is the reliable way to see what actually runs. (Source: Fedora bash.spec, Fedora which2.sh, CentOS Stream 9 bash.spec / which2.sh, retrieved 2026-09-25)

type -a cd
type -a ls
cd is a shell builtin
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls

type -a lists every match — aliases, shell builtins, and executables in your PATH. The first line is what actually runs. (Both /usr/bin/ls and /bin/ls appear on systems where /bin is a link to /usr/bin.)

Inside a shell script, use command -v to check whether a command is available. It is defined by POSIX.

command -v git
command -v cd
/usr/bin/git
cd

When nothing matches, it prints nothing and returns exit code 1, so you can use it in a condition: if command -v git >/dev/null 2>&1; then ... fi.


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

Conclusion: whereis finds the binary, man page, and source at once, ignoring your PATH.

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"

Conclusion: locate queries a prebuilt database to find any file by name, very 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 the plocate package (sudo apt install plocate / sudo dnf install plocate). See "Common Questions and Pitfalls" for how this differs between distributions.


4. Choosing the Right Command

Conclusion: Use which for paths, whereis for related files, locate to search names.

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
Find out what a name really is (alias, builtin) type -a

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

Conclusion: When which is empty, use whereis or locate; run updatedb for fresh data.

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

On current major distributions, locate comes from the plocate package. plocate is a compatible reimplementation of the older mlocate, so locate and updatedb work the same way.

# Ubuntu 22.04 and later / Debian 12 and later
sudo apt install plocate

# Fedora 36 and later / Rocky Linux 10, AlmaLinux 10, etc.
sudo dnf install plocate

On older distributions, use the mlocate package.

# Ubuntu 20.04 / Debian 11, etc.
sudo apt install mlocate

# Rocky Linux 9 / AlmaLinux 9, etc.
sudo dnf install mlocate
Distribution Package that provides locate
Ubuntu 20.04 / Debian 11 mlocate (plocate is also available on Debian 11)
Ubuntu 22.04 / Debian 12 plocate (mlocate is a transitional dummy package that pulls in plocate)
Ubuntu 24.04 and later / Debian 13 plocate (no mlocate package)
Fedora 36 and later plocate (replaces mlocate; the two cannot be installed together)
Rocky Linux 9 / AlmaLinux 9 mlocate
Rocky Linux 10 / AlmaLinux 10 plocate

(Source: Ubuntu Packages Search: mlocate / plocate, Launchpad: mlocate in Ubuntu Focal, Debian package search: mlocate / plocate, Fedora Changes/Plocate as the default locate implementation, Rocky Linux 9 BaseOS / 10 BaseOS, AlmaLinux 9 BaseOS / 10 BaseOS, retrieved 2026-09-25)


Next Reading

Share this article

Next steps