Locate vs Find in Linux: Key Differences Explained
Both find and locate are essential
command-line utilities used to search for files in the Linux operating
system, but they operate on fundamentally different mechanisms. The
primary difference is that find searches the live file
system in real time, making it thorough and current but potentially
slow, whereas locate searches a pre-built background
database, making it nearly instantaneous but prone to missing newly
created or deleted files. Understanding how each tool functions helps
determine which command is right for specific administrative and
day-to-day tasks.
How the find Command
Works
The find command traverses the actual directory tree on
your storage drive at the moment of execution. It does not rely on an
index; it actively inspects directory contents and inode metadata to
match your query.
- Real-Time Accuracy: Because it scans the live file
system,
findalways reflects the current state of your files, including files created seconds prior. - Extensive Filtering:
findsupports deep filtering criteria, including file size (-size), modification time (-mtime), file permissions (-perm), ownership (-user,-group), and file type (-type). - Direct Actions: It allows executing commands
directly on the results using the
-execor-deleteflags, eliminating the need to pipe results to tools likexargs. - Performance Impact: On large file systems or slow
storage drives, searching with
findcan consume significant I/O and take minutes to complete.
How the locate
Command Works
The locate command (often provided by
mlocate or plocate) queries a generated
database—typically located at
/var/lib/mlocate/mlocate.db—rather than the actual storage
drive.
- Instant Execution: Because it only reads from a
compact index file,
locatereturns search results almost immediately, even across millions of files. - Database Dependency: The results are only as fresh
as the database. If a file was created after the latest database update,
locatewill not find it. Conversely, if a file was deleted, it may still appear in the results. - Database Updates: The database is usually updated
once a day via an automated cron job or systemd timer running the
updatedbcommand. Users can manually refresh the index by runningsudo updatedb. - Limited Criteria:
locateprimarily matches strings against file paths and file names. It lacks the ability to filter natively by permissions, timestamps, or file sizes.
Direct Comparison
| Feature | find |
locate |
|---|---|---|
| Search Mechanism | Scans the live file system | Queries an indexed database |
| Speed | Slower (depends on disk size/speed) | Extremely fast (near instant) |
| Data Freshness | 100% real-time | Stale until updatedb
runs |
| System Resource Usage | High disk I/O during execution | Low during search; high during scheduled indexing |
| Filter Flexibility | High (time, size, owner, type, permissions) | Low (name and path pattern matching) |
| Execution on Output | Built-in (via -exec) |
Requires piping to external tools |
| Default Availability | Pre-installed on all POSIX/Linux systems | Often requires separate installation
(plocate) |
When to Use Which Command
Use locate when you need to quickly
track down configuration files, documentation, or installed program
paths where exact real-time accuracy is not critical. It is the best
choice for fast, general discovery across the entire system.
Use find when you need up-to-the-minute
accuracy, when you need to isolate files by attributes other than their
names (such as finding all files modified in the last 24 hours or files
over 500 MB), or when you need to perform batch actions on matching
files directly.