find and locate commands. It explains when to use each tool, demonstrates a short root-level demo, and provides practical find examples you can adapt on RHEL or other Linux distributions.
Why use these tools?
findsearches the filesystem in real time and is precise and flexible.locateis much faster because it queries a pre-built database rather than scanning the whole disk.
find for accuracy and complex filtering. Use locate for speed when a periodically updated index is sufficient.
The
updatedb command refreshes the database used by locate. On many systems this runs automatically (cron or systemd timers), but you can run updatedb manually to get the latest results. It typically requires root privileges to index the full filesystem.
- Create a directory and a file.
- Try
locate(it won’t find the file until the database is updated). - Run
updatedb, then uselocateagain. - Demonstrate
findexamples for real-time search.
locate
- Very fast because it uses an indexed database.
- By default
locateis case-sensitive. Use-ifor case-insensitive searches:
- Keep in mind
locateresults reflect the state of the last database update; newly created files may not appear until afterupdatedbruns.
find (real-time, powerful)
Basic syntax:
- Start directory then expression:
find <start-dir> <expression>
- Examples below assume you are searching from the current directory
.or root/.
find options and examples
Practical
find examples
findsupports many more predicates: permissions (-perm), modification time (-mtime,-mmin), pruning directories you want to skip (-prune), and executing commands on matches (-exec,-ok).- For complex tasks, combine
findwithxargs,-print0, or-exec ... +for efficient processing.
find is extremely flexible — use man find to explore predicates like -mtime, -perm, -prune, and -exec. For large searches consider -xdev to avoid crossing filesystem boundaries, and -mount on some systems.
Links and references
man find— local manual page: runman find- GNU findutils manual: https://www.gnu.org/software/findutils/manual/
man updatedb/man locate— local manual pages for locate and updatedb- RHEL documentation: https://access.redhat.com/documentation/
- Use
locatewhen you want very fast filename lookups and can tolerate slightly out-of-date results; refresh its database withupdatedbas needed. - Use
findfor accurate, on-the-fly searches and advanced filtering. It’s slower but far more powerful.