Frequently Asked Question
When should I use find versus locate?
find walks the filesystem in real time, opening every directory below the
starting point and testing each entry against the criteria you give. That makes it
slow on large trees but extraordinarily flexible: you can match on name, type,
size, owner, permissions, modification time, depth, and you can act on each match
with -exec or -delete. A typical query looks like find . -name '*.log' -mtime +7 -delete. Because it sees the filesystem as it is now, results are always
current.
locate queries a pre-built database (/var/lib/mlocate/mlocate.db or
plocate.db) that a cron or systemd-timer job rebuilds nightly with updatedb.
It is essentially instant, milliseconds to search the whole filesystem, but it
is only as current as the last index rebuild, and it does not know about new files
until then. Use locate when you want to find a file by partial name fast; use
find when you need precise predicates or fresh results.