Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.
locate - Fast File Name Search Using Database
The locate command is a high-performance file search utility that finds files by name using a pre-built database, making it significantly faster than find for simple name searches. It uses indexed databases (typically created and maintained by updatedb) to provide near-instantaneous search results across the entire filesystem. The locate command is ideal for frequent filename searches, system administration tasks, and quickly locating files when you know their names or patterns. It trades real-time accuracy for speed, as the database may not contain recently created or modified files until it's updated.
Basic Syntax
locate [OPTIONS] PATTERN...
Core Search Options
Basic Pattern Matching
-i, --ignore-case- Perform case-insensitive matching-b, --basename- Match only the basename (last component) of path names-c, --count- Only print number of matching entries, not file names-w, --wholename- Match the entire path name (default behavior)-e, --existing- Report only files that currently exist at the time locate is run
Limiting and Output Control
-l, --limit N- Limit output to N entries (same as -n)-n, --limit N- Limit output to N entries (same as -l)-0, --null- Separate output entries with ASCII NUL character instead of newline-q, --quiet- Quiet mode; do not write error messages about files that cannot be accessed-s, --stdio- Ignored for compatibility with BSD locate-S, --statistics- Print statistics about each read database instead of searching for files
Regular Expression Matching
-r, --regexp REGEXP- Search using basic regular expressions--regex- Search using extended regular expressions
Database Selection
-d, --database DBPATH- Search in DBPATH; multiple paths separated by colons-h, --help- Display help information-V, --version- Display version information
Usage Examples
Basic File Searching
Simple Pattern Matching
# Find files by exact name
locate myfile.txt
# Find files containing pattern anywhere in path
locate "project"
# Find files with wildcard patterns
locate "*.conf"
locate "config*"
locate "*.log"
# Find files matching multiple patterns
locate "httpd" "apache" "nginx"
Case-Insensitive Searching
# Case-insensitive search (very common use case)
locate -i README
locate -i "*.TXT"
locate -i "configuration"
# Find configuration files regardless of case
locate -i ".conf"
locate -i "config"
# Case-insensitive search with patterns
locate -i "*.doc" "*.pdf"
Base Name Matching
# Match only file names, not full paths (faster)
locate -b "config"
locate -b "passwd"
locate -b "hosts"
# Find specific executable names
locate -b "python"
locate -b "gcc"
locate -b "java"
# Combine with case-insensitive
locate -b -i "README"
Advanced Pattern Searching
Regular Expression Searches
# Files ending with specific extension
locate -r '\.log$' # Files ending with .log
locate -r '\.conf$' # Files ending with .conf
locate -r '\.(jpg|png|gif)$' # Image files
# Files starting with pattern
locate -r '^/etc/.*' # Files in /etc directory
locate -r '^/home/user/.+' # Files in user's home directory
locate -r '^/var/log/.*' # Log files in /var/log
# Complex pattern matching
locate -r '/home/.*/(documents|photos)/.*\.jpg'
locate -r '^/usr/bin/.*python*'
locate -r 'error.*\.(log|txt)$'
# Files with specific naming patterns
locate -r 'config\.(default|example|sample)$'
locate -r '\.bak\.(\d{4}-\d{2}-\d{2})$'
Path-Based Searching
# Files in specific directories
locate -r '^/etc/apache2/.*\.conf$'
locate -r '^/var/log/.*error.*\.log$'
locate -r '^/home/user/.*\.(doc|pdf|txt)$'
# Exclude certain directories
locate -r '^/etc/(?!apache2).*\.conf$' # All conf files except apache2
# Files in multiple locations
locate -r '^/(usr|opt)/local/bin/.*'
Output Control and Filtering
Limiting Results
# Show only first 10 matches
locate -n 10 "*.log"
locate -l 20 "*.conf"
# Count matching files without listing them
locate -c "*.txt"
locate -c "\.py$"
# Combine with other options
locate -c -i "*.conf" # Count config files case-insensitively
locate -n 5 -b "config" # Show first 5 files named config
Existence Verification
# Report only files that currently exist
locate -e "*.tmp"
locate -e "/var/log/*.log"
# Useful for cleanup scripts
locate -e "*.bak" | xargs rm
Null-Separated Output
# Use null character as separator (safer for file processing)
locate -0 "*.txt" | xargs -0 rm
locate -0 "*.log" | xargs -0 ls -la
# Process files with special characters in names
locate -0 "*\ *" | xargs -0 -I {} echo "Found: {}"
Database Management Operations
Using Custom Databases
# Search specific database
locate -d /custom/path/mlocate.db pattern
locate -d /var/lib/mlocate/mlocate.db "config"
# Search multiple databases
locate -d /var/lib/mlocate/mlocate.db:/backup/mlocate.db pattern
# Search database with relative path
locate -d ./mydb.db "myfile"
Database Information
# Show database statistics
locate --statistics
locate -S
# Show statistics for custom database
locate -d /custom/db --statistics
# Database statistics output includes:
# - Database path
# - Number of entries
# - File size
# - Last update time
Database Creation and Maintenance
Database Updates
Standard Database Updates
# Update system-wide database (requires root privileges)
sudo updatedb
# Update with verbose output
sudo updatedb -v
# Update specific directory only
sudo updatedb -o /custom/mlocate.db -U /path/to/directory
# Update with custom configuration file
sudo updatedb --output=/tmp/test.db --prunepaths="/tmp,/var/tmp"
Advanced Database Configuration
# Create database for specific user directory
updatedb -o ~/.mlocate.db -U ~/Documents
# Create database with custom prune paths
sudo updatedb --prunepaths="/media,/mnt,/tmp" \
--prunefs="nfs,nfs4,smbfs,cifs" \
--output=/custom/mlocate.db
# Create multiple databases for different purposes
sudo updatedb -o /var/lib/mlocate/system.db -U /
updatedb -o ~/.local/userfiles.db -U ~/Documents
Database Configuration
Configuration File Management
# Main configuration file (typically /etc/updatedb.conf)
# Common configuration options:
# Paths to exclude from database
PRUNEPATHS="/tmp /var/spool /media /var/tmp /var/cache"
# Filesystem types to exclude
PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs coda devpts ftpfs devfs mfs shfs sysfs cifs lustre"
# Whether to prune bind mounts
PRUNE_BIND_MOUNTS="yes"
# Network filesystems to exclude
NETFS="nfs nfs4 cifs smbfs afs"
Custom Configuration Examples
# /etc/updatedb.conf for development server
PRUNEPATHS="/tmp /var/tmp /var/cache /media /mnt /var/spool/clientmqueue"
PRUNEFS="NFS nfs nfs4 smbfs cifs afs"
PRUNE_BIND_MOUNTS="yes"
# Environment-specific database configuration
sudo updatedb --prunepaths="/var/cache,/tmp,/var/tmp" \
--prunefs="nfs,nfs4,smbfs" \
--output=/var/lib/mlocate/production.db
Practical Applications
System Administration
Configuration File Management
# Find all system configuration files
locate -r '^/etc/.*\.conf$'
# Find specific service configurations
locate -r '/etc/(apache2|nginx|httpd)/.*\.conf$'
locate -r '/etc/(mysql|postgresql)/.*\.conf$'
# Find SSH configuration files
locate -r 'ssh.*config'
locate -b -i "sshd_config"
# Find all init/systemd service files
locate -r '^/(etc|usr/lib)/systemd/.*\.service$'
locate -r '^/etc/init\.d/.*'
Log File Analysis
# Find all log files on system
locate -r '\.log$'
# Find web server logs
locate -r '^/var/log/(apache2|nginx|httpd)/.*\.log$'
locate -b "access.log"
locate -b "error.log"
# Find system logs by date pattern
locate -r '^/var/log/.*\.log\.\d{4}-\d{2}-\d{2}$'
locate -r '^/var/log/.*\.log\.\d+$'
# Find application logs
locate -r '^/var/log/(mysql|postgresql|redis)/.*\.log$'
Security Auditing
# Find world-writable files (requires post-processing)
locate -r '.*' | xargs -I {} sh -c 'test -w "{}" && echo "{}"'
# Find files with SUID bit (combine with find)
locate -b "passwd" | xargs ls -l 2>/dev/null | grep -E '^...s'
# Find configuration files that might contain passwords
locate -r '\.conf$' | xargs grep -l "password\|secret\|key" 2>/dev/null
# Find backup files that might contain sensitive data
locate -r '\.(bak|backup|old)$'
locate -r '\.(pgp|gpg)$'
Development Workflow
Source Code Navigation
# Find all source files of specific type
locate -r '\.(c|cpp|h|hpp)$'
locate -r '\.(py|pyx|pyi)$'
locate -r '\.(java|scala|kt)$'
locate -r '\.(js|ts|jsx|tsx)$'
# Find configuration files in projects
locate -r '\.(json|yaml|yml|toml|ini)$'
locate -b "package.json"
locate -b "requirements.txt"
locate -b "Dockerfile"
# Find build and make files
locate -b "Makefile"
locate -b "CMakeLists.txt"
locate -r '\.(mk|build)$'
Documentation and README Files
# Find documentation files
locate -b -i "readme"
locate -b -i "changelog"
locate -b -i "license"
locate -b -i "install"
# Find documentation in various formats
locate -r '\.(md|txt|rst|tex|pdf)$'
locate -r '^/usr/share/doc/.*'
# Find man pages
locate -r '^/usr/share/man/man.*\.gz$'
Testing and Validation
# Find test files
locate -r 'test.*\.(py|js|java)$'
locate -r '\.(test|spec)\.(js|ts)$'
locate -b -i "test"
# Find build output directories
locate -b "build"
locate -b "dist"
locate -b "target"
# Find temporary and cache files
locate -r '\.(tmp|temp|cache)$'
locate -b "__pycache__"
locate -b "node_modules"
Data Management
Media File Organization
# Find image files by type
locate -r '\.(jpg|jpeg|png|gif|bmp|tiff|webp)$'
locate -r '\.(raw|cr2|nef|arw)$' # Raw image formats
# Find audio files
locate -r '\.(mp3|wav|flac|ogg|m4a|aac|wma)$'
# Find video files
locate -r '\.(mp4|avi|mkv|mov|wmv|flv|webm|m4v)$'
# Find document files
locate -r '\.(pdf|doc|docx|xls|xlsx|ppt|pptx)$'
locate -r '\.(odt|ods|odp)$' # OpenDocument formats
Backup and Archive Operations
# Find all archive files
locate -r '\.(zip|tar|gz|bz2|xz|7z|rar)$'
# Create backup of configuration files
locate -r '^/etc/.*\.conf$' | tar -czf config_backup.tar.gz -T -
# Find and process backup files
locate -r '\.bak\.' | xargs ls -la
locate -r '\.(\d{4}-\d{2}-\d{2})$'
# Find databases and data files
locate -r '\.(sqlite|db|mdb)$'
locate -r '\.(sql|dump)$'
Advanced Usage Techniques
Performance Optimization
Efficient Searching Patterns
# Use base name matching for better performance
locate -b "python" # Faster than
locate "python" # This
# Use specific patterns over broad regex when possible
locate "*.conf" # Faster than
locate -r '\.conf$' # This
# Limit results for large searches
locate -n 100 "*.log" # Prevent excessive output
locate -n 50 -b "config"
# Combine search terms for precision
locate "/etc/" "*.conf" # Multiple patterns
Database Performance Tuning
# Optimized database creation for specific use cases
sudo updatedb --prunepaths="/var/cache,/tmp,/var/tmp,/media,/mnt" \
--prunefs="nfs,nfs4,smbfs,cifs,iso9660,udf" \
--output=/var/lib/mlocate/optimized.db
# Create minimal database for security scanning
sudo updatedb -U /etc -o /etc/etcfiles.db
locate -d /etc/etcfiles.db "*.conf"
# Create user-specific database for faster personal searches
updatedb -U ~/Documents -o ~/docs.db
locate -d ~/docs.db "project"
Integration with Other Commands
Pipeline Operations
# Count files by type
locate "*.log" | wc -l
locate "*.conf" | wc -l
locate "*.py" | wc -l
# Find and process files
locate "*.tmp" | xargs rm -f
locate "*.bak" | xargs -I {} mv {} {}.old
# Search within found files
locate "*.py" | xargs grep -l "TODO\|FIXME"
locate "*.conf" | xargs grep -H "server"
# Create archives of found files
locate -0 "*.txt" | xargs -0 tar -czf text_files.tar.gz
locate "*.jpg" | zip -@ images.zip
Complex Scripting
#!/bin/bash
# Advanced file cleanup script
echo "Finding temporary files..."
TEMP_FILES=$(locate -0 "*.tmp" "*.temp" 2>/dev/null)
if [ -n "$TEMP_FILES" ]; then
echo "Found temporary files:"
echo "$TEMP_FILES" | tr '\0' '\n'
read -p "Delete these files? (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "$TEMP_FILES" | xargs -0 rm -f
echo "Cleanup completed."
fi
else
echo "No temporary files found."
fi
# Find large log files and compress them
echo "Finding large log files..."
locate "*.log" | xargs -I {} sh -c 'if [ -s "{}" ] && [ $(stat -c%s "{}") -gt 1048576 ]; then echo "Compressing {}"; gzip "{}"; fi'
System Monitoring
# Monitor database size and age
ls -lh /var/lib/mlocate/mlocate.db
stat /var/lib/mlocate/mlocate.db
# Find recently indexed files (after database update)
sudo updatedb
# ... make changes ...
# To see what changed, compare old vs new database
# Regular monitoring script
#!/bin/bash
# Daily system file monitor
DB_SIZE=$(stat -c%s /var/lib/mlocate/mlocate.db 2>/dev/null || echo 0)
echo "Locate database size: $((DB_SIZE/1024/1024)) MB"
CONFIG_FILES=$(locate -c -r '^/etc/.*\.conf$')
echo "Configuration files found: $CONFIG_FILES"
LOG_FILES=$(locate -c '\.log$')
echo "Log files found: $LOG_FILES"
Database Optimization Strategies
Custom Database Creation
Specialized Databases
# Create database for web server files
sudo updatedb -U /var/www -o /var/lib/mlocate/webfiles.db
locate -d /var/lib/mlocate/webfiles.db "*.php"
# Create database for development directory
updatedb -U ~/projects -o ~/.mlocate/projects.db
locate -d ~/.mlocate/projects.db "*.py"
# Create database for user home directory
updatedb -U ~ -o ~/.mlocate/home.db
locate -d ~/.mlocate/home.db "document"
# Create database for system binaries only
sudo updatedb -U /bin /sbin /usr/bin /usr/sbin -o /var/lib/mlocate/binaries.db
locate -d /var/lib/mlocate/binaries.db "python"
Environment-Specific Databases
# Production server database (excludes development directories)
sudo updatedb --prunepaths="/home,/tmp,/var/tmp,/opt/dev,/var/cache" \
--prunefs="nfs,nfs4,smbfs,cifs,debugfs" \
--output=/var/lib/mlocate/production.db
# Development server database (includes development paths)
sudo updatedb --prunepaths="/tmp,/var/tmp,/var/cache" \
--output=/var/lib/mlocate/development.db
# Security-focused database (includes all paths)
sudo updatedb --prunepaths="/tmp,/var/tmp" \
--output=/var/lib/mlocate/security.db
Maintenance Automation
Scheduled Database Updates
#!/bin/bash
# Advanced database maintenance script
# System database update
echo "Updating system locate database..."
sudo updatedb -v
# Update custom databases
echo "Updating custom databases..."
# Web files database
if [ -d "/var/www" ]; then
echo "Updating web files database..."
sudo updatedb -U /var/www -o /var/lib/mlocate/webfiles.db
fi
# User projects database
if [ -d "$HOME/projects" ]; then
echo "Updating projects database..."
updatedb -U "$HOME/projects" -o "$HOME/.mlocate/projects.db"
fi
# Clean old databases
find ~/.mlocate/ -name "*.db" -mtime +30 -delete 2>/dev/null
# Database statistics
echo "Database statistics:"
locate -S
echo "Database maintenance completed."
Database Cleanup
# Remove corrupted databases
sudo rm /var/lib/mlocate/mlocate.db
sudo updatedb
# Database size monitoring
#!/bin/bash
DB_FILE="/var/lib/mlocate/mlocate.db"
MAX_SIZE=$((500*1024*1024)) # 500MB
if [ -f "$DB_FILE" ]; then
CURRENT_SIZE=$(stat -c%s "$DB_FILE")
if [ $CURRENT_SIZE -gt $MAX_SIZE ]; then
echo "Database is large ($(stat -c%s "$DB_FILE" | numfmt --to=iec)), rebuilding..."
sudo updatedb
fi
fi
Security and Privacy Considerations
Access Control
Permission-Based Filtering
# Locate respects file permissions
# Regular users only see files they have permission to access
# Check what files are accessible vs what's in database
sudo locate -c "*" # All files in database
locate -c "*" # Files accessible to current user
# Find files you might not be able to see
# (This will show only what locate can access)
locate -r '^/root/.*' 2>/dev/null
Privacy Configuration
# Exclude sensitive directories from indexing
sudo updatedb --prunepaths="/home,/root,/var/lib,/var/private" \
--output=/var/lib/mlocate/public.db
# Create database that excludes user data
# Add to /etc/updatedb.conf:
PRUNEPATHS="/tmp /var/tmp /media /home /root"
# Verify what's being excluded
sudo updatedb --debug 2>&1 | grep -i prune
Security Auditing
Finding Sensitive Files
# Find files that might contain sensitive information
locate -b -i "password" | head -20
locate -b -i "secret" | head -20
locate -b -i "key" | head -20
# Find configuration files with security implications
locate -r '\.(pem|key|crt|p12)$'
locate -r '\.gpg$'
locate -r '\.pgp$'
# Check for backup files in web-accessible locations
locate -r '^/var/www/.*\.(bak|backup|old)$'
Monitoring System Changes
# Baseline creation
sudo updatedb -o /root/baseline.db
locate -d /root/baseline.db -c "*" > /root/baseline.count
# Change detection
sudo updatedb -o /root/current.db
CURRENT_COUNT=$(locate -d /root/current.db -c "*")
# Compare (basic example)
if [ "$CURRENT_COUNT" -ne "$(cat /root/baseline.count)" ]; then
echo "File count has changed since baseline"
fi
Troubleshooting Common Issues
Database Problems
Corrupted Database
# Symptoms: locate returns no results or errors
# Solution: Rebuild database
sudo rm -f /var/lib/mlocate/mlocate.db
sudo updatedb
# Alternative: Use specific database path
sudo updatedb --output=/var/lib/mlocate/mlocate.db
Outdated Database
# Recent files not found
# Solution: Update database
sudo updatedb
# Check database age
stat /var/lib/mlocate/mlocate.db
# Automated update scheduling
sudo crontab -e
# Add: 0 2 * * * /usr/bin/updatedb
Permission Issues
# "permission denied" errors
# Check database permissions
ls -la /var/lib/mlocate/mlocate.db
# Fix permissions if necessary
sudo chmod 644 /var/lib/mlocate/mlocate.db
sudo chown root:root /var/lib/mlocate/mlocate.db
# Database not accessible to user
# Create user-specific database
updatedb -o ~/.mlocate/user.db -U ~
locate -d ~/.mlocate/user.db pattern
Performance Issues
Slow Searches
# Database too large or fragmented
# Check database statistics
locate -S
# Rebuild optimized database
sudo updatedb
# Use more specific search patterns
locate -b "config" # Faster than broad search
locate -n 100 "*.log" # Limit results
Memory Issues
# Low memory systems
# Limit database update resource usage
sudo updatedb --prunepaths="/tmp,/var/tmp,/var/cache"
# Use smaller, specialized databases
updatedb -U ~/Documents -o ~/docs.db # Instead of full system
Search Result Issues
Too Many Results
# Limit output
locate -n 50 "*.log"
# Use more specific patterns
locate -r '^/var/log/apache2.*\.log$' # Instead of just *.log
# Use base name matching
locate -b "error" # Instead of searching all paths
No Results Found
# Check database exists and is readable
ls -la /var/lib/mlocate/mlocate.db
# Update database
sudo updatedb
# Check search pattern
locate -c "*" # Count total entries
# Use case-insensitive search
locate -i "Config"
Integration with Modern Workflows
DevOps and Automation
Infrastructure Monitoring
#!/bin/bash
# Configuration file drift detection
CONFIG_DB="/var/lib/mlocate/configs.db"
# Update config database
sudo updatedb -U /etc -o "$CONFIG_DB"
# Find new configuration files
locate -d "$CONFIG_DB" -r '^/etc/.*\.conf$' > /tmp/current_configs.txt
# Compare with baseline
if [ -f "/etc/configs_baseline.txt" ]; then
diff /etc/configs_baseline.txt /tmp/current_configs.txt
fi
Docker and Container Environments
# Create database for container-specific files
sudo updatedb -U /var/lib/docker -o /var/lib/mlocate/docker.db
locate -d /var/lib/mlocate/docker.db "*.json"
# Find container configuration files
locate -r '^/var/lib/docker/.*config\.json$'
# Monitor container image storage
locate -r '^/var/lib/docker/overlay2/.*/diff$'
Cloud Computing
AWS/GCP Integration
# Find AWS credential files
locate -r '\.aws/(credentials|config)$'
# Find Google Cloud credentials
locate -r '\.gcloud/.*\.json$'
locate -r '\.config/gcloud/.*\.json$'
# Find Terraform state files
locate -r '\.terraform/.*\.tfstate$'
locate -r '\.tfstate$'
# Find Kubernetes configuration
locate -r '\.kube/.*config$'
locate -r 'kubeconfig.*'
Backup Script Integration
#!/bin/bash
# Smart backup using locate for file discovery
# Source code backup
echo "Backing up source code..."
PROJECT_FILES=$(locate -r '\.(py|js|java|cpp|c|h)$' | grep -E "$HOME/(projects|src)")
echo "$PROJECT_FILES" | tar -czf "source_backup_$(date +%Y%m%d).tar.gz" -T -
# Configuration backup
echo "Backing up configurations..."
CONFIG_FILES=$(locate -r '^/etc/.*\.conf$')
echo "$CONFIG_FILES" | tar -czf "config_backup_$(date +%Y%m%d).tar.gz" -T -
# Database backup
echo "Backing up databases..."
DB_FILES=$(locate -r '\.(sqlite|db|mdb)$' 2>/dev/null)
if [ -n "$DB_FILES" ]; then
echo "$DB_FILES" | tar -czf "database_backup_$(date +%Y%m%d).tar.gz" -T -
fi
echo "Backup completed successfully."
Related Commands
find- Find files with complex criteria and real-time resultsupdatedb- Create and update locate databaseswhich- Locate command executables in PATHwhereis- Find command binaries and documentationgrep- Search within file contentsmlocate- Alternative implementation with security featuresslocate- Secure locate with additional filteringfzf- Interactive fuzzy finder (can use locate output)
Best Practices
- Update databases regularly for accurate search results
- Use specific patterns to limit result sets and improve performance
- Create specialized databases for different purposes (web files, projects, etc.)
- Use -n limit option when searching for common patterns
- Combine with other commands for powerful file processing pipelines
- Use base name matching (-b) for faster searches when appropriate
- Monitor database size and rebuild periodically if needed
- Configure prune paths appropriately to exclude unnecessary directories
- Use case-insensitive search (-i) when file naming is inconsistent
- Employ locate for filename searches and
findfor attribute-based searches
Performance Tips
- Base name searches are significantly faster than full path searches
- Regular expressions are slower than simple pattern matching
- Limiting results prevents overwhelming output on common searches
- Multiple small databases can be more efficient than one large database
- Update frequency should balance freshness against system load
- Memory usage scales with database size; consider specialized databases
- SSD storage improves database update and search performance
- Database location affects search speed (local SSD vs network storage)
- System load affects database updates; schedule during off-peak hours
- Pattern specificity impacts search performance; be as specific as possible
The locate command provides an exceptionally fast method for filename searching by leveraging pre-built indexed databases. While it sacrifices real-time accuracy for speed, it excels at quick system-wide file discovery, configuration management, and routine administrative tasks. When used effectively with proper database maintenance and appropriate search patterns, locate becomes an indispensable tool for system administrators, developers, and power users who frequently need to locate files by name.