The mlocate command is a powerful and secure implementation of the traditional locate utility in Linux systems. Unlike its predecessor, mlocate provides enhanced security features and faster search capabilities by maintaining an indexed database of file system contents. This comprehensive guide will explore every aspect of mlocate, from basic usage to advanced configuration techniques.
What is mlocate?
mlocate (merging locate) is a secure locate implementation that creates and maintains a database of files and directories on your Linux system. It’s designed to be faster and more secure than the original locate command, providing quick file searches without traversing the entire file system each time.
Key Features of mlocate
- Security: Respects file permissions and only shows files accessible to the current user
- Performance: Uses an indexed database for lightning-fast searches
- Regular Updates: Automatically updates the database through cron jobs
- Pattern Matching: Supports various pattern matching techniques
- Case Sensitivity: Configurable case-sensitive and case-insensitive searches
Installing mlocate
Most modern Linux distributions come with mlocate pre-installed. If it’s not available on your system, you can install it using your distribution’s package manager.
Ubuntu/Debian Installation
sudo apt update
sudo apt install mlocate
CentOS/RHEL/Fedora Installation
# CentOS/RHEL
sudo yum install mlocate
# Fedora
sudo dnf install mlocate
Arch Linux Installation
sudo pacman -S mlocate
Basic mlocate Usage
The basic syntax of mlocate is straightforward:
locate [OPTIONS] PATTERN
Simple File Search
To search for a file named “config.txt”:
locate config.txt
Example Output:
/etc/ssh/ssh_config.txt
/home/user/Documents/config.txt
/opt/application/config.txt
/var/log/config.txt.backup
Wildcard Searches
Use wildcards to broaden your search:
locate "*.log"
Example Output:
/var/log/auth.log
/var/log/syslog
/var/log/boot.log
/home/user/application.log
Essential mlocate Options
Case-Insensitive Search (-i)
Perform case-insensitive searches:
locate -i README
This will find files like “readme”, “README”, “Readme”, etc.
Limit Results (-l)
Limit the number of results returned:
locate -l 5 *.txt
This returns only the first 5 matching results.
Count Results (-c)
Count the number of matching files without displaying them:
locate -c "*.pdf"
Example Output:
247
Basename Search (-b)
Search only the basename of files (excluding directory path):
locate -b config
This finds files named exactly “config” rather than files containing “config” in their path.
Advanced Search Patterns
Regular Expression Search (-r)
Use regular expressions for complex pattern matching:
locate -r ".*\.log$"
This finds all files ending with “.log”.
Exact Match Search
Use the --regex option with anchors for exact matches:
locate --regex "^/home/user/Documents/.*\.txt$"
Multiple Pattern Search
Combine multiple patterns:
locate -r "(config|settings).*\.ini$"
This finds files starting with “config” or “settings” and ending with “.ini”.
Database Management
Understanding the mlocate Database
The mlocate database is typically stored at /var/lib/mlocate/mlocate.db. This database contains indexed information about all files and directories on your system.
Manual Database Update
Update the database manually using the updatedb command:
sudo updatedb
This process may take several minutes depending on your system size and disk speed.
Check Database Status
View database information:
locate --statistics
Example Output:
Database /var/lib/mlocate/mlocate.db:
8,121 directories
64,213 files
2,876,542 bytes used to store database
Configuration and Customization
Configuration File
The main configuration file is located at /etc/updatedb.conf. Key configuration options include:
# View current configuration
cat /etc/updatedb.conf
Example Configuration:
PRUNE_BIND_MOUNTS="yes"
PRUNENAMES=".git .bzr .hg .svn"
PRUNEPATHS="/tmp /var/spool /media /var/lib/os-prober"
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc"
Excluding Directories
Modify PRUNEPATHS to exclude specific directories from indexing:
PRUNEPATHS="/tmp /var/spool /media /home/user/large_archive"
Excluding File Systems
Exclude specific file system types:
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc"
Security Features
Permission-Based Results
mlocate respects file permissions and only shows files accessible to the current user:
# As regular user
locate /root/
This typically returns no results or limited results based on permissions.
Secure Mode
By default, mlocate runs in secure mode, which means:
- Only shows files readable by the current user
- Respects directory permissions
- Updates database with proper privilege separation
Automation and Scheduling
Automatic Updates via Cron
Check existing cron job for automatic database updates:
cat /etc/cron.daily/mlocate
Custom Update Schedule
Create a custom cron job for more frequent updates:
# Edit crontab
sudo crontab -e
# Add entry for hourly updates
0 * * * * /usr/bin/updatedb
Performance Optimization
Optimizing Search Speed
Several factors affect mlocate performance:
- Database Size: Smaller databases search faster
- Pattern Specificity: More specific patterns return results quicker
- Regular Updates: Keep database current for accurate results
Monitoring Database Size
ls -lh /var/lib/mlocate/mlocate.db
Example Output:
-rw-r----- 1 root mlocate 2.8M Aug 25 08:15 /var/lib/mlocate/mlocate.db
Troubleshooting Common Issues
Database Not Found Error
If you encounter “Database not found” error:
# Manually create the database
sudo updatedb
# Check if mlocate is properly installed
which locate
which updatedb
Permission Denied
For permission issues:
# Check database permissions
ls -la /var/lib/mlocate/
# Ensure proper group membership
groups $(whoami)
Outdated Results
If results seem outdated:
# Check last update time
stat /var/lib/mlocate/mlocate.db
# Force manual update
sudo updatedb
Best Practices
Efficient Search Strategies
- Use specific patterns: Narrow down searches with specific file extensions or directories
- Combine options: Use multiple options together for precise results
- Regular updates: Maintain current database for accurate searches
- Security awareness: Understand permission-based result filtering
System Maintenance
- Monitor database size regularly
- Adjust PRUNEPATHS for system-specific needs
- Schedule updates during low-usage periods
- Test configuration changes thoroughly
Comparison with Other Search Tools
mlocate vs find
| Feature | mlocate | find |
|---|---|---|
| Speed | Very Fast (indexed) | Slower (real-time) |
| Real-time Results | No (database-dependent) | Yes |
| System Load | Low | Higher |
| Pattern Matching | Good | Excellent |
When to Use Each Tool
- Use mlocate for: Quick filename searches, regular file location tasks
- Use find for: Complex searches with multiple criteria, real-time accuracy needs
Conclusion
The mlocate command is an essential tool for efficient file management in Linux systems. Its combination of speed, security, and ease of use makes it invaluable for system administrators and users alike. By understanding its features, configuration options, and best practices, you can leverage mlocate to significantly improve your file search workflows.
Remember to keep your database updated regularly and configure exclusions appropriately for your system’s needs. With proper setup and maintenance, mlocate becomes a powerful ally in navigating complex file systems quickly and securely.
Whether you’re a beginner learning Linux commands or an experienced administrator optimizing system performance, mastering mlocate will enhance your productivity and system management capabilities.







