首页 > Sysadmin > 正文

Linux File Location Commands: find, which, and locate Compared

2026-11-01 Sysadmin

Not sure where a file is? find is versatile but slow, which only locates executables, and locate relies on a database for fastest results. Everything below has been tested on WSL2.

1. TL;DR: Choose the Right Command for the Job

  • Looking for any file: find /path -name "filename". Full recursive search, slowest but most accurate.
  • Looking for executables: which <cmd>. Searches only PATH, ideal for confirming command paths.
  • Fast filename search: locate <keyword>. Uses a pre-built database for instant results, though they may be stale.

2. find: Versatile Recursive Search

find /home -name "report.pdf" -type f
find /var/log -name "*.log" -mtime -7
find / -path "*/nginx/*" -name "*.conf" 2>/dev/null

find searches recursively from a given starting point, using test conditions like -name, -path, and -mtime to filter results [1]. -type f restricts results to regular files, avoiding interference from directories or device nodes. 2>/dev/null suppresses permission-denied errors. -mtime -7 is shorthand for “modified within the last 7 days”.

3. which: Executables Only

which python3
which -a python3

which searches the PATH in order and returns the first matching executable path [3]. The -a flag prints every match, which is handy for spotting same-name binaries in different paths. Note that which does not recognize shell built-ins (like cd), functions, or aliases.

4. locate: Database Prefix Matching

locate report.pdf
locate -i nginx.conf
locate -r '/etc/nginx.*.conf$'

locate searches a filename database updated on a schedule by updatedb; it doesn’t scan the disk, so it’s extremely fast [2]. Matching is prefix-based by default, -i ignores case, and -r switches to regex mode. The downside is newly created or recently deleted files may not have made it into the database yet, so results can be stale.

5. Environmental and Permission Differences

Environment find which locate
Physical server Normal Normal Normal, database updates daily
WSL2 Normal Normal Run sudo updatedb first; default database is older
Container Normal Normal Usually no scheduled updatedb; update manually
Restricted account Affected by permissions Works only within PATH Depends on database permissions

In containers and CI environments without a scheduled updatedb job, locate results lag behind; in those situations, find is the better choice.

6. Troubleshooting and Limitations

Quick diagnostics

Symptom Cause Fix
find produces no output Wrong starting directory or filename typo Use ls /path to verify directory contents
find reports Permission denied Entered a directory without access Add 2>/dev/null or use -readable
which returns empty Command not in PATH, or it is an alias Use type <cmd> to check real type
locate misses new files Database hasn’t been updated Run sudo updatedb manually
find returns too many results Scope too broad or missing -type Narrow the starting point or add -type f

Limitations

  • find becomes very slow with deep recursion and isn’t suited for frequent full-disk searches.
  • which only checks PATH and cannot locate config or data files.
  • locate depends on database freshness; new or deleted files may have a delay.
  • On remote servers without sudo, updatedb may not run.

Verified test record

  • Environment: WSL2 Ubuntu 24.04, kernel 5.15, find (GNU findutils) 4.9.0.
  • find /tmp -name testfile001 -type f: successfully returned ./testfile001.
  • which find: returned /usr/bin/find.
  • locate: not installed by default; after manual install, run updatedb to search newly created files.

References

Commands tested on WSL Ubuntu 24.04 and cross-checked with Debian-based and general Linux distribution documentation.