locate, which, and whereis - Three Ways to Find Commands in Linux
What You Will Learn
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
whichto find the executable path of a command - Use
whereisto find the binary, man page, and source at once - Use
locateto 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?"
which?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)
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"
which. What does whereis add?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"
locate for?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.
Case-insensitive search
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
| 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
pythonPython 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?
which node and got nothing. I thought I installed it...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