Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Concepts

find vs locate

Compare find and locate: live directory walking versus a prebuilt database, speed, freshness, filters, permissions, and when fd, which or type are better choices.

Linux has two main ways to search for files by name, and they work in opposite ways. find walks the directory tree right now and tests every entry against conditions you describe. locate searches a prebuilt database of file paths that a scheduled job (updatedb) refreshes, usually once a day.

That single design difference explains everything else: locate answers in milliseconds but can be stale and only matches names; find is slower on large trees but always current and can test size, age, owner, permissions and more, and even act on the results.

Terminal
find /var/log -name '*.log' -mtime -1   # live walk with conditions
locate nginx.conf                       # instant lookup in the index

Try it

Bash find sees the present

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Why locate can be stale (read-only idea, simulated)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

The sandbox has no locate command or database, so the second example imitates it. On a real system, run sudo updatedb to refresh the index by hand and use locate -S to see its size and age.

Side by side

find locate
Data source The live filesystem A database built by updatedb
Freshness Always current As old as the last updatedb (often a day)
Speed Proportional to the size of the tree Near instant
Matches Name, path, type, size, times, owner, group, permissions, inode, regex... Mostly path substring, glob or regex on the name
Actions -exec, -delete, -print0, -ls Prints names only
Needs Nothing installed beyond findutils The plocate or mlocate package and its database
Scope Any directory you can read, one tree at a time Everything indexed (system-wide)
Permissions Only what you can read right now Respects permissions of the index (plocate and mlocate hide what you could not see)
Network/removable disks Walks them if you point it there Often excluded by PRUNEFS config

When to use which

  • "Where is that config file?" locate. A quick, forgiving search by part of a name across the whole system.
  • "Files changed in the last hour", "bigger than 100 MB", "owned by nobody", "world-writable": find, because only it understands attributes.
  • "Delete or process all matches": find ... -exec or -print0 | xargs -0.
  • "Just created it and can't find it": find; the index does not know it yet.
  • Inside a project: git ls-files (tracked files) or grep -r/rg for contents are often better than either.
  • Looking for a command, not a file: command -v, type, which and whereis search PATH and standard locations, which is much faster and exact.

Usage notes

  • Limit the walk. find / -name x visits the whole system; start from the smallest sensible directory, add -xdev to stay on one filesystem, and -prune to skip big trees like node_modules.
  • Quote glob patterns passed to -name so the shell does not expand them first (-name '*.c').
  • locate patterns: with no wildcard, locate foo matches any path containing foo; with wildcards it needs a full match. -i ignores case, -r takes a regex, -b matches only the base name, -c counts.
  • updatedb configuration lives in /etc/updatedb.conf (paths and filesystems to skip). Results of locate can reveal that a file existed even after it is gone.
  • Different implementations: plocate (fast, common on modern Debian and Fedora), mlocate (older), GNU slocate. The options are mostly compatible.
  • Scripts should prefer find. Their correctness cannot depend on when a cron job last ran.
  • Modern alternatives: fd (a friendlier find) and fzf (fuzzy picker) are popular, but are not installed by default and not available everywhere.