Level 1

find locate and which practical guide

Maximilian B. 5 min read 56 views

When you are new to Linux, file searching looks simple until you work on a real server. You need one command to find where a binary is in your PATH, another command to walk the filesystem safely, and another command to return results quickly from an index. That is where which, find, and locate fit.

This guide explains what each command does, where it can mislead you, and how to use it on current systems: Debian 13.3, Ubuntu 24.04.3 LTS, Ubuntu 25.10, Fedora 43, RHEL 10.1, and RHEL 9.7. The commands here are written for entry-level technicians, but the examples reflect production work.

what each command is for

find locate and which practical guide visual summary diagram
Visual summary of the key concepts in this guide.

Use the commands for different questions:

  • which: "Which executable will run if I type this command?"
  • find: "Search the live filesystem right now using rules."
  • locate: "Search a prebuilt filename database very fast."

If you mix these up, you can make bad decisions. Example: using locate to check if a file currently exists can fail if the index is old. Using find / during business hours can spike disk I/O and slow customer traffic. The right command is partly about speed and partly about operational risk.

using which without getting tricked

which checks your PATH and prints the executable that will run first. This is useful for debugging version conflicts, especially when a tool was installed from both the package manager and a manual download.

# Show the executable path used by your shell session
which python3
which nginx

# Show all matches in PATH order
which -a python3

Important limit: which does not fully describe shell behavior. Shell builtins, aliases, and functions can bypass normal executable lookup. In Bash, use type -a when you need the complete picture.

# Better command origin check in Bash
alias ll='ls -alF'
type -a ll
type -a cd
type -a systemctl

Production consequence: if an automation script runs a different binary than expected, patching or security steps may target the wrong version. A quick which -a or type -a check before rollout can prevent that.

using find for accurate live searches

find reads the filesystem in real time, so results are current. It is slower than locate but more reliable for immediate state checks. Start from the smallest path you can. Avoid scanning the whole disk unless there is no alternative.

# Find nginx config files under /etc
find /etc -type f -name "*nginx*.conf"

# Case-insensitive search for log files modified in the last day
find /var/log -type f -iname "*.log" -mtime -1

# Find large files for disk cleanup triage
find /var -type f -size +500M -printf "%s %p\n" | sort -nr | head

Safe deletion pattern for beginners:

  1. Run find without delete options and review output.
  2. Add a restrictive condition (time, size, name, or path).
  3. Only then run -delete or -exec rm.
# Step 1: preview
find /tmp -type f -name "app-cache-*" -mtime +7

# Step 2: delete only after review
find /tmp -type f -name "app-cache-*" -mtime +7 -delete

Operator note: on busy hosts, recursive searches can compete with databases and web services for disk reads. Schedule heavy find jobs in low-traffic windows, or limit them to known directories.

using locate for speed and understanding updatedb

locate is fast because it reads from an index, not from live directory traversal. It is ideal when you need quick filename discovery across large systems, such as finding all paths that include php.ini or sshd_config.

# Fast filename lookups from the locate database
locate sshd_config
locate -i php.ini
locate -n 20 backup

But speed comes with a tradeoff: results can be stale until the database is refreshed. Most systems refresh automatically via timer or cron, but not all minimal installs do this by default. If results look wrong, update the index manually.

# Rebuild locate index (requires root)
sudo updatedb

# Common troubleshooting check: file exists now?
# Verify with test/find if locate result seems outdated
test -f /etc/ssh/sshd_config && echo "exists"

Security and privacy note: updatedb can be configured to skip paths such as temporary mounts or sensitive trees. In shared environments, review /etc/updatedb.conf (or distribution equivalent) so indexes do not expose paths that regular users should not enumerate.

compatibility notes for current distributions

For Debian 13.3, Ubuntu 24.04.3 LTS, Ubuntu 25.10, Fedora 43, RHEL 10.1, and RHEL 9.7, core find behavior is consistent because it comes from GNU findutils. The option style used in this guide works across these releases.

which is also available on these families, but shell-native type remains the better diagnostic command for aliases and functions.

For locate, package naming can differ by distribution or image profile. Some systems provide plocate, others still expose compatibility around mlocate. The user command remains locate, and database refresh remains updatedb. If locate: command not found appears, install the locate package for your distro and enable its timer/cron update path.

# Debian/Ubuntu family (typical)
sudo apt update
sudo apt install plocate

# Fedora/RHEL family (package naming may vary by repo)
sudo dnf install plocate || sudo dnf install mlocate

On production RHEL 9.7 and RHEL 10.1 fleets, confirm package policy in your internal repositories before writing scripts. Hardcoding one package name across environments is a common automation failure.

practical workflow you can reuse

A simple decision flow works well in daily operations:

  1. Need command origin in your current shell: start with type -a, then which -a.
  2. Need exact current filesystem state: use find in a narrow path.
  3. Need fast broad filename discovery: use locate, then verify critical paths with find or test.

For beginners, this avoids confusion. For operators, it reduces incidents caused by stale indexes, wrong binaries, or heavy disk scans at the wrong time. Use each tool for its real design goal, and your troubleshooting will be both faster and safer.

Share this article
X / Twitter LinkedIn Reddit