locate
Overview
The locate
command finds files and directories by searching a pre-built database. It’s much faster than find
for simple filename searches but requires an updated database.
Syntax
locate [options] pattern...
Common Options
Option | Description |
---|---|
-i |
Ignore case |
-l n |
Limit output to n entries |
-c |
Count matches only |
-b |
Match basename only |
-r |
Use regex patterns |
-e |
Check file existence |
-A |
All patterns must match |
-0 |
Separate output with null |
--database=path |
Use specific database |
Key Use Cases
- Quick file location
- Find system files
- Locate configuration files
- Search for executables
- Find documentation
Examples with Explanations
Example 1: Basic Search
locate filename
Finds all files containing ‘filename’ in their path
Example 2: Case Insensitive
locate -i README
Finds files with ‘readme’, ‘README’, ‘ReadMe’, etc.
Example 3: Limit Results
locate -l 10 *.conf
Shows only first 10 configuration files
Database Management
The locate database is typically updated by updatedb
:
sudo updatedb
Database locations: - /var/lib/mlocate/mlocate.db
(most systems) - /var/lib/locate/locatedb
(older systems)
Common Usage Patterns
Find config files:
locate -i config | grep -E '\.(conf|cfg)$'
Search in specific directory:
locate /etc/ | grep ssh
Count occurrences:
locate -c "*.log"
Advanced Searching
Regex patterns:
locate -r '\.py$'
Multiple patterns:
locate -A pattern1 pattern2
Basename only:
locate -b '\filename'
Performance Analysis
- Extremely fast searches
- No filesystem traversal needed
- Database must be current
- Memory efficient
- Good for frequent searches
Additional Resources
Best Practices
- Update database regularly
- Use with grep for filtering
- Consider file existence with -e
- Use case-insensitive search when needed
- Limit results for large searches
Database Configuration
Configuration file: /etc/updatedb.conf
- PRUNE_BIND_MOUNTS - PRUNEFS (filesystems to skip) - PRUNENAMES (directories to skip) - PRUNEPATHS (paths to skip)
Security Considerations
- Database shows all accessible files
- May reveal system structure
- Regular users see only accessible files
- Consider privacy implications
- Database updates require root
Troubleshooting
- Database not updated (run updatedb)
- File not found (recently created)
- Permission issues
- Database corruption
- Pattern syntax errors
Integration Examples
With xargs:
locate "*.tmp" | xargs rm
With grep:
locate python | grep bin
Scripting:
if locate -q myfile; then echo "Found"; fi