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.
find /var/log -name '*.log' -mtime -1 # live walk with conditions locate nginx.conf # instant lookup in the index
Try it
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.
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 ... -execor-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) orgrep -r/rgfor contents are often better than either. - Looking for a command, not a file:
command -v,type,whichandwhereissearchPATHand standard locations, which is much faster and exact.
Usage notes
- Limit the walk.
find / -name xvisits the whole system; start from the smallest sensible directory, add-xdevto stay on one filesystem, and-pruneto skip big trees likenode_modules. - Quote glob patterns passed to
-nameso the shell does not expand them first (-name '*.c'). locatepatterns: with no wildcard,locate foomatches any path containingfoo; with wildcards it needs a full match.-iignores case,-rtakes a regex,-bmatches only the base name,-ccounts.updatedbconfiguration lives in/etc/updatedb.conf(paths and filesystems to skip). Results oflocatecan reveal that a file existed even after it is gone.- Different implementations:
plocate(fast, common on modern Debian and Fedora),mlocate(older), GNUslocate. The options are mostly compatible. - Scripts should prefer
find. Their correctness cannot depend on when a cron job last ran. - Modern alternatives:
fd(a friendlierfind) andfzf(fuzzy picker) are popular, but are not installed by default and not available everywhere.