Linux which, whereis, locate: 3 Commands to Find a File's Location (Path)

Sometimes you need to edit httpd.conf but don't know where the file is, or a command with the same name exists in two places and you're not sure which one runs. The Linux which command finds the file that actually runs, the Linux whereis command finds the executable plus config folders and manuals, and the locate command finds every path containing the name. This post compares the three in a table and lets you try them on a virtual server in your browser.
On AlmaLinux 9.8, which comes in the which 2.21 package and whereis in util-linux 2.37.4, so both work out of the box, while locate needs the mlocate 0.26 package installed separately.
✅ which vs whereis vs locate
The three commands look in different places. which and whereis search fixed folders on the spot, while locate looks up names in a file list (DB) built in advance.
| Command | What it finds | Where it looks | New files |
|---|---|---|---|
| which | The one command file that runs, plus aliases | Folders listed in PATH | Found right away |
| whereis | Executable · config folders · manuals | Fixed folders such as /usr/bin · /etc · /usr/share/man | Found right away |
| locate | Every file and folder containing the name | A prebuilt DB (/var/lib/mlocate/mlocate.db) | Found after updatedb |
Search for the same httpd with which and whereis and the difference is obvious.
~]# which httpd
/usr/sbin/httpd
~]# whereis httpd
httpd: /usr/sbin/httpd /usr/lib64/httpd /etc/httpd /usr/share/httpd /usr/share/man/man8/httpd.8.gzlocate shows every path that matches even part of the name, so typing [locate httpd.conf] also returns the manual (httpd.conf.5.gz) and any backup copies, not just the config file.
On the virtual server below, pick which, whereis or locate on the left to change the chips; clicking a chip types that command. You can also type it on your keyboard.
To find files by size or modification date instead of by name, use the find command; to see the path of the folder you're in, use the pwd command.
✅ Linux which Command
which searches the folders in PATH from the front, one by one, and shows the first executable it finds. That's the file that actually runs when you type the command.
On AlmaLinux, bash passes the alias list to which, so for a command with an alias you get both the alias and the actual file. If it's nowhere in PATH, you get [no command in (PATH)].
~]# which ll
alias ll='ls -l --color=auto'
/usr/bin/ls
~]# which htop
/usr/bin/which: no htop in (/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin)In practice -a is the only option. When the same name exists in two PATH folders it shows both, and the one at the top is what actually runs.
~]# which ping
/usr/sbin/ping
~]# which -a ping
/usr/sbin/ping
/usr/bin/pingOn AlmaLinux 9.8, /usr/sbin/ping is a link pointing to /usr/bin/ping, so the result is the same. A program you installed yourself in /usr/local/bin runs before the one in /usr/bin because of the PATH order.
[which cd] shows /usr/bin/cd, but when you type cd in the shell, the shell builtin runs instead of this file. Check whether a command is a builtin with [type cd], which prints [cd is a shell builtin].
✅ Linux whereis Command
whereis uses the command name to find the executable and its manuals together. It ignores PATH and searches fixed folders such as /usr/bin · /usr/sbin · /usr/lib64 · /etc · /usr/share, so even a config folder with the same name (/etc/httpd) shows up.
| Option | Description | |
|---|---|---|
| -b | Binaries only. For httpd: /usr/sbin/httpd and /usr/lib64/httpd · /etc/httpd · /usr/share/httpd | |
| -m | Manuals only (/usr/share/man · /usr/share/info) | |
| -s | Sources only. On a default AlmaLinux 9.8 install you get just the name, like [httpd:] | |
| -l | List of folders whereis searches |
~]# whereis -b httpd
httpd: /usr/sbin/httpd /usr/lib64/httpd /etc/httpd /usr/share/httpd
~]# whereis -m httpd
httpd: /usr/share/man/man8/httpd.8.gz
~]# whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1ossl.gz /usr/share/man/man1/passwd.1.gzIf nothing is found you get just the name, like [htop:]. which returns exit code 1 when it finds nothing while whereis returns 0, so use which to check in a script whether something is installed.
✅ Linux locate Command
locate doesn't search the disk; it looks up names in a prebuilt DB. That makes it fast, but it can't find files created after the DB was built.
~]# dnf -y install mlocate
~]# updatedbIf you run it right after installing, there's no DB yet and you get [locate: can not stat () `/var/lib/mlocate/mlocate.db’: No such file or directory]; running updatedb once creates it. After that, mlocate-updatedb.timer rebuilds the DB once a day.
A newly created file doesn't show up until updatedb runs, and a deleted file keeps showing up until then. To see only files that exist right now, add -e.
~]# touch report.txt
~]# locate report.txt
~]# updatedb
~]# locate report.txt
/root/work/report.txt
~]# rm -f report.txt
~]# locate report.txt
/root/work/report.txt
~]# locate -e report.txtPRUNEPATHS in /etc/updatedb.conf includes /tmp · /var/tmp · /mnt · /media, so files in these folders don't show up even after running updatedb.
🔲 locate Command Options
| Option | Description | |
|---|---|---|
| -i | Ignore case | |
| -c | Only the number of matches instead of paths | |
| -l= -n | Add a number to limit the results to that many. -l 3 | |
| -b | Match only the file name at the end of the path | |
| -e | Leave out files that are in the DB but no longer exist | |
| -S | DB info (folders · files · size) | |
| -r | Search with a regular expression. locate -r “httpd.conf$” |
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.