Skip to main content

Updatedb

The locate command is entirely dependent on a pre-built database. The utility responsible for crawling the filesystem and building this database is updatedb.

Because it requires permission to read the entire filesystem, updatedb must be run as root (or via sudo).

The Standard Update

If you just installed a new package or downloaded a massive dataset and want to find it using locate, you must refresh the index manually:

sudo updatedb

By default, this command runs silently. Depending on the speed of your storage (NVMe vs HDD) and the total number of files, this can take anywhere from 2 seconds to several minutes.

Monitoring Progress (Verbose Mode)

To see exactly what directories updatedb is currently crawling, use the -v (verbose) flag:

sudo updatedb -v

This is incredibly useful if updatedb appears to hang, as it will reveal if it is stuck crawling a slow network mount or a massively deep directory structure like a node_modules cache.

Database Location and Age

The database file is usually stored in /var/lib/ under either mlocate or plocate, depending on your distribution.

# Ubuntu/Debian (mlocate)
ls -lh /var/lib/mlocate/mlocate.db

# Ubuntu 22.04+ (plocate)
ls -lh /var/lib/plocate/plocate.db

Checking When It Was Last Updated

You can verify how fresh your locate results are by checking the modification time of the database file:

stat /var/lib/mlocate/mlocate.db | grep Modify

The Automatic Cron Schedule

You usually don't need to run updatedb manually because Linux distributions install a cron job that runs it automatically.

This job is typically located in /etc/cron.daily/mlocate. Because it's in the cron.daily directory, it will execute once per day, usually early in the morning (e.g., 6:00 AM) depending on your system's anacron or systemd-timers configuration.

Increasing Update Frequency

If your system's files change rapidly and a 24-hour delay is unacceptable, you can move the update schedule.

  1. Remove the daily job:
    sudo rm /etc/cron.daily/mlocate
  2. Add a new cron job for the root user that runs every hour:
    sudo crontab -e
    # Add this line to run at the top of every hour:
    # 0 * * * * /usr/bin/updatedb

[!WARNING] Running updatedb too frequently on systems with very slow HDDs or massive amounts of file churn can cause noticeable I/O spikes. Monitor your disk usage with iotop.

Managing Multiple Databases

By default, updatedb scans from the root directory / and stores everything in the global database. However, you can create isolated databases for specific directories.

This is highly recommended for massive network storage mounts (NFS/CIFS) or external backup drives, so they don't slow down the main system index.

Step 1: Build a Custom Database

Use --database-root to specify where to start crawling, and --output to specify where to save the database file.

# Index ONLY the /mnt/archive drive, save it to /tmp/archive.db
sudo updatedb --database-root /mnt/archive --output /tmp/archive.db

Step 2: Query the Custom Database

When you want to search, tell locate to use your custom database file via the -d flag.

# Search the custom database
locate -d /tmp/archive.db "2023_financials.pdf"

You can even query both the main system database and your custom one simultaneously by separating them with a colon:

locate -d /var/lib/mlocate/mlocate.db:/tmp/archive.db "report"