updatedb Command in Linux



The updatedb command in Linux is a very handy tool. It updates the file database for the locate command, which helps in finding files on the system. This command examines the system's directories and makes a list, or index, of all the filenames. This index lets users search for files quickly, so they don't have to scan the entire disk every time they look for something. It saves a lot of time when trying to find where files are located.

The updatedb command comes with the locate package. It works well by only indexing files that have been changed recently. This way, it ensures that the search results are accurate and up to date.

Table of Contents

Here is a comprehensive guide to the options available with the updatedb command −

Understanding updatedb Command

Instead of the old method of searching for files manually across the entire system, updatedb makes an index stored at /var/lib/mlocate/mlocate.db. This index helps the locate program to find files much faster than searching directly on the disk.

Typically, updatedb updates automatically every day through a scheduled task known as a cron job. However, when there are significant changes with files, you can manually run updatedb to refresh the index and keep it up to date.

Installing updatedb Command

Before using updatedb, you must ensure that the mlocate package is installed on your system. This package provides both updatedb and locate, essential for maintaining and searching the indexed file database.

For Debian / Ubuntu-based systems

sudo apt install locate -y

For CentOS / RHEL-based systems

sudo yum install locate -y

For Fedora

sudo dnf install locate -y

For Arch Linux

sudo pacman -S locate

Syntax of updatedb Command

The basic syntax of updatedb is straightforward −

updatedb [options]

Where, [options] modify the behavior of the update process.

updatedb Command Options

The updatedb command supports multiple options that help refine and control the indexing process.

Options Description
--prunepaths="dir1 dir2" Excludes specific directories from indexing, such as /mnt or /media.
--prunenames="file1 file2" Prevents indexing of files that match certain names.
--output=db_file Allows users to store the indexed database in a custom location instead of the default /var/lib/mlocate/mlocate.db.
--database-root=/path Specifies a different root directory for indexing, allowing users to scan only a portion of the file system rather than indexing everything.
--verbose Displays detailed output while running updatedb, showing real-time indexing information.
--findoptions='-option1 -option2...' Specifies extra options for the find command used internally by updatedb.
--localpaths='dir1 dir2...' Defines directories on local file systems that should be scanned and included in the indexing database.
--netpaths='dir1 dir2...' Similar to --localpaths, but applies to network-mounted directories instead of local storage.
--prunepaths='dir1 dir2...' Excludes specific directories from indexing, reducing unnecessary data inclusion.
--prunefs='fs1 fs2...' Prevents certain file systems from being indexed, ensuring updatedb does not scan unneeded storage types.
--output=dbfile Allows users to save the index database to a custom file instead of the default /var/lib/mlocate/mlocate.db.
--netuser=user Defines the user identity used when scanning network paths.
--localuser=user Similar to --netuser, but applies to indexing local directories, ensuring scans adhere to user-specific permissions.
--dbformat Sets the format of the database file. This controls how indexed entries are stored, ensuring compatibility with different versions of locate.

Examples of updatedb Command in Linux

Let's explore a few practical examples of updatedb command on Linux system −

  • Refresh File Index Manually
  • Excluding Specific Directories from Indexing
  • Storing the File Index Database in a Custom Location
  • Checking Indexed Files

Refresh File Index Manually

To manually update the file database for accurate locate searches, use the following command −

sudo updatedb

This command scans the system and updates the file database immediately, ensuring newly added or modified files can be quickly located.

Excluding Specific Directories from Indexing

If you want to prevent certain directories from being indexed −

sudo updatedb --prunepaths="/tmp /var/tmp ...;"

This command excludes directories that contain removable drives or temporary files, improving search speed by avoiding unnecessary indexing.

Storing the File Index Database in a Custom Location

To save the updated database file in a different directory, use the following option with updatedb

sudo updatedb --output=/home/user/custom_db.db

This command changes the storage location of the indexed database, allowing users to create multiple file databases for customized searches.

Checking Indexed Files

Once the database is updated, users can quickly retrieve a file's location using locate −

locate myfile.txt

This command searches the indexed database for myfile.txt, providing instant results without scanning the entire file system.

Conclusion

The updatedb command is crucial for keeping a file database current, which allows users to search quickly and efficiently with the locate command. By keeping the file index updated, users can find files faster and improve how organized their systems are.

You can choose to run updatedb manually, ignore directories you don't need, or adjust where the index is stored. Understanding how to effectively use this tool boosts your ability to manage files across various Linux systems.

Advertisements