The locate command in Linux is used to quickly search files and directories using a prebuilt database instead of scanning the entire filesystem. This makes it significantly faster than the find command for name-based searches. In this guide, you will learn locate syntax, key options, and real-world use cases with a quick cheat sheet.
locate Command Cheat Sheet
| Task | Command |
|---|---|
| Search file by name | locate filename |
| Case-insensitive search | locate -i pattern |
| Limit results | locate -n 10 pattern |
| Count matching files | locate -c pattern |
| Show only existing files | locate -e pattern |
| Use regex search | locate -r pattern |
| Match basename only | locate -b pattern |
| Update locate database | sudo updatedb |
| Show database statistics | locate -S |
| Use custom database | locate -d DBPATH pattern |
What is locate command in Linux
The locate command in Linux is used to quickly search files and directories based on their names using a prebuilt database. Unlike traditional search tools, it does not scan the filesystem in real time, which makes it extremely fast for large systems.
How locate works using database (mlocate / plocate)
The locate command relies on a database (typically maintained by mlocate or plocate) that stores indexed file paths. This database is updated periodically using the updatedb command.
sudo updatedbWhen you run locate, it searches this database instead of scanning directories, resulting in faster output.
Why locate is faster than find command
locate is faster than find because it searches a prebuilt database rather than traversing the filesystem. However, this also means that locate may not show recently created or deleted files until the database is updated.
locate vs find command (Key Differences)
The locate and find commands are both used to search files in Linux, but they work differently and serve different use cases.
When to use locate vs find
Use locate when:
- You need fast file search based on name
- You are working with large filesystems
- Real-time accuracy is not critical
Use find when:
- You need up-to-date results
- You want to search by size, permissions, or modification time
- You need advanced filtering and actions
Limitations of locate command
The locate command has some limitations due to its dependency on a database:
- May not show recently created files
- May include deleted files if database is outdated
- Limited to name-based searches
- Requires periodic database updates
Install and set up locate command
Before using the locate command, you may need to install and initialize it depending on your Linux distribution.
Install locate on Ubuntu, CentOS, RHEL
Install plocate (modern replacement for mlocate):
sudo apt install plocate # Ubuntu/Debian
sudo dnf install plocate # RHEL/CentOS/FedoraIf plocate is not available, install mlocate:
sudo apt install mlocate
sudo yum install mlocateInitialize and update database using updatedb
After installation, initialize or update the database:
sudo updatedbThis ensures that locate returns accurate results. The database is usually updated automatically via cron jobs.
Search files quickly using locate
The locate command is primarily used for fast file searches based on name patterns.
Search files by name pattern
Search for files matching a specific name:
locate filenameExample:
locate passwdPerform case-insensitive searches using -i
Use -i to ignore case sensitivity:
locate -i patternExample:
locate -i logLimit search results using -n
Limit the number of results returned:
locate -n 10 patternExample:
locate -n 5 *.confImprove accuracy of locate results
The locate command relies on a database, so results may sometimes be outdated. These options help improve accuracy and filter relevant results.
Show only existing files using -e
The -e option ensures that only files currently present in the filesystem are displayed. This helps avoid showing deleted files that still exist in the database.
locate -e patternExample:
locate -e passwdWhy locate shows deleted files (database issue)
Since locate uses a prebuilt database, it may still show files that have been deleted but are present in the database.
To fix this, update the database:
sudo updatedbRegular database updates ensure accurate search results.
Perform advanced searches using locate
The locate command supports advanced search capabilities using patterns and regular expressions to refine results.
Use regex pattern with -r
The -r option allows you to search using regular expressions for more precise matching.
locate -r patternExample:
locate -r '\.log$'Match exact file names vs partial matches
By default, locate returns partial matches. To match exact filenames, use regex anchors.
locate -r '/filename$'Example:
locate -r '/passwd$'This ensures only exact matches are returned.
Optimize and manage locate database
Managing the locate database helps improve performance and accuracy of search results.
Check database statistics using -S
The -S option displays statistics about the locate database, including number of indexed files and database size.
locate -SUse custom database with -d
You can specify a custom database instead of the default one.
locate -d DBPATH patternExample:
locate -d /tmp/custom.db testReduce noise using --quiet option
The --quiet (or -q) option suppresses error messages such as permission denied.
locate -q patternThis helps produce cleaner output.
Frequently Asked Questions
1. What is locate command in Linux?
locate is a Linux command used to quickly find files using a prebuilt database instead of scanning the filesystem.2. Why is locate faster than find command?
locate searches a prebuilt database, while find scans the filesystem in real time, making locate significantly faster.3. How to update locate database?
Use sudo updatedb to manually update the locate database.4. Why locate does not show recent files?
locate relies on a database that may not be updated immediately, so recently created files may not appear.5. What is the difference between locate and find command?
locate is faster but may show outdated results, while find is slower but always shows real-time and accurate results.Summary
The locate command is a fast and efficient tool for searching files in Linux using a prebuilt database. While it offers excellent performance compared to find, it requires periodic database updates to ensure accuracy. By understanding key options like -i, -n, -e, and -r, you can perform both simple and advanced file searches effectively.
Official Documentation
For more details, refer to the official documentation:



