Understanding mlocate and updatedb in Linux
This article provides an overview of the mlocate package
in the Linux operating system, explaining how the updatedb
command functions and why its underlying database is essential for fast
file indexing. You will learn how mlocate differs from
traditional search utilities, how the indexing process operates under
the hood, and how system administrators configure and maintain this
database for efficient file location.
What is mlocate?
mlocate (Merging Locate) is an indexing and search
utility designed to quickly find files and directories across a Linux
filesystem. Unlike real-time search tools such as find,
which actively scan the storage drive at the moment a command is
executed, mlocate queries a pre-compiled index database.
This approach allows users to run the locate command and
receive nearly instantaneous search results, regardless of the size or
complexity of the directory tree.
The Role of updatedb
The index queried by locate does not update
automatically whenever a file is created, renamed, or deleted. Instead,
it relies on the updatedb command to refresh its
records.
When executed, updatedb performs the following
tasks:
- Filesystem Traversal: It scans local drives and mounted filesystems to capture the current state of files, directories, and paths.
- Database Generation: It compiles this filesystem
structure into an optimized, compact binary database, typically located
at
/var/lib/mlocate/mlocate.db. - Incremental Merging: As the "m" in
mlocateimplies,updatedbmerges new filesystem states with existing database data rather than rebuilding the entire structure from scratch, conserving disk input/output (I/O) and processor resources.
Security and Permission Awareness
A critical feature that sets mlocate apart from older
implementations of locate is permission handling.
Traditional locate databases could inadvertently expose
sensitive filenames to unauthorized users.
mlocate stores file permissions within the database.
When an unprivileged user executes a query, locate verifies
whether that user has read access to the parent directories before
displaying the matching filenames in the output. This ensures that
privacy and system integrity remain intact without sacrificing search
speed.
Automation via Cron and Systemd
Because manual updates can be easily forgotten, Linux distributions
routinely automate updatedb via scheduled background
tasks.
- Cron Jobs: Many distributions place a shell script
inside
/etc/cron.daily/mlocateto runupdatedbonce per day. - Systemd Timers: Modern distributions often use
systemd timers (such as
updatedb.timerandupdatedb.service) to trigger updates during low-activity periods.
If a file was created after the most recent automated scan, it will
not appear in search results until updatedb is run again,
either automatically by the scheduler or manually by a user with root or
sudo privileges.
Configuration and Optimization
The behavior of updatedb is governed by the
configuration file /etc/updatedb.conf. This file allows
administrators to fine-tune how the database is populated using several
key directives:
PRUNEFS: Defines filesystem types that should not be indexed, such as temporary filesystems (tmpfs), network shares (nfs,cifs), or virtual filesystems (proc,sysfs).PRUNEPATHS: Lists specific directory paths to exclude from indexing, such as/tmp,/var/spool, or/media.PRUNENAMES: Specifies folder names (such as.gitor.svn) thatupdatedbshould skip entirely.
By excluding volatile, network-mounted, or unnecessary paths,
updatedb minimizes system resource consumption during
updates and keeps the resulting database compact and fast to query.