updatedb Command
Update the database used by the locate command to enable fast file and directory searching across the filesystem.
Syntax
updatedb [OPTIONS]
The updatedb command scans the filesystem and updates the database that the locate command uses to quickly find files and directories.
Common Options
| Option |
Description |
--localpaths='paths' |
Scan only specified paths |
--netpaths='paths' |
Scan network paths |
--prunepaths='paths' |
Skip specified paths |
--prunefs='fs' |
Skip specified filesystem types |
--output=file |
Write database to specified file |
--database-root=path |
Set root directory for scanning |
--verbose |
Show verbose output |
--debug-pruning |
Debug pruning decisions |
--help |
Display help information |
Basic Usage
Standard database update
# Update the locate database (requires root)
sudo updatedb
# Update with verbose output
sudo updatedb --verbose
# Check when database was last updated
ls -la /var/lib/mlocate/mlocate.db
# Test the updated database
locate filename
Basic updatedb operations for maintaining the locate database
Database information
# Check database location and size
ls -lh /var/lib/mlocate/mlocate.db
ls -lh /var/lib/locate/locatedb # Alternative location
# Check database age
stat /var/lib/mlocate/mlocate.db
# View database statistics
locate --statistics
# Count entries in database
locate '*' | wc -l
Check database status and information
Configuration
updatedb configuration file
# View current configuration
cat /etc/updatedb.conf
# Example configuration file
# /etc/updatedb.conf
PRUNE_BIND_MOUNTS="yes"
PRUNEFS="9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
PRUNENAMES=".git .hg .svn CVS"
PRUNEPATHS="/tmp /var/spool /media /var/lib/os-prober /var/lib/ceph"
# Edit configuration
sudo nano /etc/updatedb.conf
Configure updatedb behavior through configuration file
Configuration options explained
Configuration Variables
- PRUNE_BIND_MOUNTS - Skip bind mounts (yes/no)
- PRUNEFS - Filesystem types to skip
- PRUNENAMES - Directory names to skip
- PRUNEPATHS - Specific paths to skip
- LOCALPATHS - Local paths to scan
- NETPATHS - Network paths to scan
Custom configuration examples
# Skip additional directories
PRUNEPATHS="/tmp /var/spool /media /mnt /opt/games /home/user/cache"
# Skip version control directories
PRUNENAMES=".git .hg .svn .bzr CVS .sass-cache node_modules"
# Skip network filesystems
PRUNEFS="nfs nfs4 cifs smbfs ncpfs"
# Scan only specific paths
LOCALPATHS="/home /usr/local /opt"
# Include network paths
NETPATHS="/mnt/nfs /media/network"
Customize updatedb configuration for specific needs
Advanced Usage
Custom database creation
# Create database for specific directory
sudo updatedb --localpaths='/home/user/projects' --output=/tmp/projects.db
# Use custom database with locate
locate -d /tmp/projects.db filename
# Create database excluding certain paths
sudo updatedb --prunepaths='/home/user/cache /home/user/.git' --output=/tmp/clean.db
# Scan only local filesystems
sudo updatedb --prunefs='nfs nfs4 cifs' --output=/tmp/local.db
# Create database with specific root
sudo updatedb --database-root=/mnt/backup --output=/tmp/backup.db
Create custom databases for specific purposes
Debugging and troubleshooting
# Debug pruning decisions
sudo updatedb --debug-pruning --verbose
# Test configuration without updating
sudo updatedb --verbose --output=/dev/null
# Check what would be pruned
sudo updatedb --debug-pruning 2>&1 | grep "pruned"
# Verify filesystem scanning
sudo updatedb --verbose 2>&1 | grep "Scanning"
# Check for permission issues
sudo updatedb --verbose 2>&1 | grep -i "permission\|denied"
Debug updatedb behavior and configuration issues
Performance optimization
# Run updatedb with lower priority
sudo nice -n 19 updatedb
# Limit I/O impact
sudo ionice -c 3 updatedb
# Combine nice and ionice
sudo nice -n 19 ionice -c 3 updatedb
# Run in background
sudo updatedb &
# Monitor updatedb progress
sudo updatedb --verbose &
tail -f /var/log/syslog | grep updatedb
Optimize updatedb performance and system impact
Automation and Scheduling
Cron scheduling
# Check existing cron job
cat /etc/cron.daily/mlocate
cat /etc/cron.daily/locate
# View system crontab
cat /etc/crontab | grep updatedb
# Custom cron job (run at 2 AM daily)
echo "0 2 * * * root /usr/bin/updatedb" | sudo tee -a /etc/crontab
# User-specific cron job
crontab -e
# Add: 0 3 * * * /usr/bin/updatedb --localpaths="$HOME" --output="$HOME/.locatedb"
# Weekly update (Sunday at 1 AM)
echo "0 1 * * 0 root /usr/bin/updatedb" | sudo tee /etc/cron.d/updatedb-weekly
Schedule automatic database updates
Systemd timer
# Check existing systemd timer
systemctl status mlocate-updatedb.timer
systemctl list-timers | grep updatedb
# Create custom systemd service
sudo tee /etc/systemd/system/updatedb-custom.service << EOF
[Unit]
Description=Update locate database
[Service]
Type=oneshot
ExecStart=/usr/bin/updatedb
Nice=19
IOSchedulingClass=3
EOF
# Create systemd timer
sudo tee /etc/systemd/system/updatedb-custom.timer << EOF
[Unit]
Description=Update locate database daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
EOF
# Enable and start timer
sudo systemctl enable updatedb-custom.timer
sudo systemctl start updatedb-custom.timer
Use systemd timers for database updates
Automated scripts
#!/bin/bash
# Smart updatedb script
LOCKFILE="/var/run/updatedb.lock"
LOGFILE="/var/log/updatedb.log"
# Check if already running
if [ -f "$LOCKFILE" ]; then
echo "updatedb already running" | tee -a "$LOGFILE"
exit 1
fi
# Create lock file
touch "$LOCKFILE"
# Function to cleanup
cleanup() {
rm -f "$LOCKFILE"
}
trap cleanup EXIT
# Log start time
echo "$(date): Starting updatedb" | tee -a "$LOGFILE"
# Check system load
LOAD=$(uptime | awk '{print $10}' | sed 's/,//')
if (( $(echo "$LOAD > 2.0" | bc -l) )); then
echo "$(date): High system load ($LOAD), skipping updatedb" | tee -a "$LOGFILE"
exit 0
fi
# Run updatedb with low priority
nice -n 19 ionice -c 3 updatedb --verbose >> "$LOGFILE" 2>&1
# Log completion
echo "$(date): updatedb completed" | tee -a "$LOGFILE"
# Rotate log if too large
if [ $(stat -f%z "$LOGFILE" 2>/dev/null || stat -c%s "$LOGFILE") -gt 1048576 ]; then
mv "$LOGFILE" "$LOGFILE.old"
fi
Create intelligent updatedb automation scripts
Multiple Databases
Creating specialized databases
# Home directory database
sudo updatedb --localpaths="$HOME" --output="$HOME/.locatedb"
locate -d "$HOME/.locatedb" filename
# Development projects database
sudo updatedb --localpaths='/home/user/projects /opt/projects' \
--prunenames='.git node_modules .sass-cache' \
--output='/var/lib/locate/projects.db'
# System files database (excluding user data)
sudo updatedb --localpaths='/ /usr /etc /var' \
--prunepaths='/home /tmp /var/tmp' \
--output='/var/lib/locate/system.db'
# Media files database
sudo updatedb --localpaths='/media /mnt /home/user/Music /home/user/Videos' \
--output='/var/lib/locate/media.db'
Create specialized databases for different purposes
Using multiple databases
# Search in specific database
locate -d /var/lib/locate/projects.db "*.py"
# Search in multiple databases
locate -d "/var/lib/locate/system.db:/var/lib/locate/projects.db" filename
# Set default database path
export LOCATE_PATH="/var/lib/locate/projects.db:$HOME/.locatedb"
locate filename
# Create database search script
#!/bin/bash
# search-all-dbs.sh
DATABASES="/var/lib/mlocate/mlocate.db:/var/lib/locate/projects.db:$HOME/.locatedb"
locate -d "$DATABASES" "$@"
Work with multiple locate databases
Troubleshooting
Common Issues
- Permission denied - updatedb requires root privileges
- Database not found - Check database location and permissions
- Slow performance - Large filesystems take time to scan
- Missing files - Files may be in pruned directories
- Outdated results - Database needs updating after file changes
Diagnostic commands
# Check if updatedb is installed
which updatedb
updatedb --version
# Check database location
locate --statistics
# Verify configuration
cat /etc/updatedb.conf
# Check cron jobs
ls -la /etc/cron.daily/ | grep -E "(locate|mlocate)"
# Check systemd timers
systemctl list-timers | grep updatedb
# Check for running updatedb processes
ps aux | grep updatedb
# Check disk space
df -h /var/lib/mlocate/
df -h /var/lib/locate/
Diagnose updatedb and locate issues
Recovery procedures
# Rebuild corrupted database
sudo rm /var/lib/mlocate/mlocate.db
sudo updatedb
# Fix permissions
sudo chown root:mlocate /var/lib/mlocate/mlocate.db
sudo chmod 640 /var/lib/mlocate/mlocate.db
# Reset configuration
sudo cp /etc/updatedb.conf /etc/updatedb.conf.backup
sudo dpkg-reconfigure mlocate # Debian/Ubuntu
# Manual database creation
sudo find / -type f -print0 2>/dev/null | \
sudo /usr/libexec/frcode > /var/lib/locate/locatedb
# Check database integrity
locate --statistics
locate '*' | head -10
Recover from database corruption or configuration issues
Best Practices
updatedb Best Practices
- Run updatedb during off-peak hours to minimize system impact
- Configure appropriate pruning to exclude unnecessary directories
- Use nice and ionice to reduce system impact
- Monitor database size and update frequency
- Create specialized databases for specific use cases
- Regularly check and maintain cron jobs or systemd timers
- Keep configuration files backed up
Security Considerations
- Privacy - Database contains all file paths, protect access
- Permissions - Ensure proper database file permissions
- Sensitive data - Exclude directories with sensitive information
- Network paths - Be cautious with network filesystem inclusion
- User access - Consider who can read the locate database