In this procedure, you will learn how to improve your file management skills on the Linux operating system with locate, a powerful command-line tool for efficient indexed file searching. The locate command can help you accelerate troubleshooting, perform system audits, and enhance everyday tasks with rapid file location capabilities.

What is ‘locate’?

‘locate’ is a command-line utility that allows you to quickly find files on Linux systems by simply searching for the file names. Unlike other search tools that traverse the entire file system in real-time, ‘locate’ relies on a regularly updated file database to perform its searches. The concept is very simple, the database contains a list of all the files on your system along with their paths. When you execute a ‘locate’ command, it searches this database by file name rather than the file system itself, resulting in significantly faster search times.

Did you know there are two versions of the locate command? There is mlocate and plocate. When you install locate, you get both versions installed. But you can optionally install either mlocate or plocate. While both mlocate and plocate help locate files on Linux systems, plocate offers superior performance and efficiency due to its optimized data structures. It generates smaller databases, conducts faster searches, and is designed for modern Linux features.

Notably, some distributions have transitioned to plocate as their default locate implementation. While mlocate still functions and can be suitable when seeking files matching any of the provided patterns, plocate is generally preferred for its speed and efficiency advantages.

How ‘locate’ Works on the Linux System

The locate command is an extremely useful utility. Before tools like it, users had to craft complex find commands to find files in Linux. Locate is clever because immediately after installation, the filesystem is quickly indexed and saved to a database. As we mentioned before, instead of searching the filesystem, the locate command searches the database manually – it’s that simple!

Let’s discover in more detail how it works:

#1: Database Creation

The file database underpinning ‘locate’ is typically generated and maintained by a system process known as ‘updatedb‘. This process runs at scheduled intervals using a crontab that’s created during installation. The update runs generally once a day, but this can easily be changed. The updatedb commands the service to systematically scan the entire file system to capture a snapshot of all files and their locations.

mlocate is fast at indexing the file system because it uses an incremental update strategy, an optimized database update structure, and minimizes its impact on the system with low-priority I/O tasks.

#2: Database Storage:

The default database storage location of ‘locate’ resides within the ‘/var/lib/mlocate/‘ directory. Should the need arise, this location can be easily changed. You can opt to save the database to an NVME or SSD storage for even quicker indexing and searches.

#3: Search Execution:

Upon executing a ‘locate’ command, it scans the pre-built database for files that align with your specified search criteria. The search results output and include the complete paths of the matching files.

The search looks for partial and an exact match, you can even include a globbing option such as regex values. The output shows on your default system standard output. (TTY stdout)

Ubuntu Install Locate – A Step-by-Step Guide

I am not aware of ‘locate’ being pre-installed in any Linux distribution. Other commands like ‘find’ are included, but in all distributions I have used, installing mlocate manually was necessary.

In this next section, we will explain how to install ‘locate’ package. Let’s dive into the process of installing and using ‘locate’ on your Ubuntu server:

Step 1 – Install Locate Command

In the majority of contemporary Linux distributions like Ubuntu, the ‘locate’ utility is not included by default. You’ll install ‘locate’ package, which provides the ‘locate’ command to usr/bin/locate and the required packages for managing the file databases.

Launch your terminal and execute the following command to install ‘locate’:

#To Install locate use (Recommended)
sudo apt install locate
#To install specifically mlocate use (Optional)
sudo apt install mlocate
#To install specifically plocate use (Optional)
sudo apt install plocate

If you have the Yum Package Manager installed you can install locate with this command line utility

sudo yum install mlocate

Step 2 – Update the Locate DB

After installing the mlocate package, you need to populate the ‘locate’ database for the first time. You can do this by running the following command as root user:

sudo updatedb

This process might take some time, depending on the size of your file system. Once it’s complete, you’re ready to start using ‘locate’.

Step 3 – Find Files Using the Locate Command

The basic syntax for using the locate command is as follows, you can also search the main pages to learn more.

locate [options] filename

man locate

Replace the filename with the name of the file you’re searching for. You can also use wildcards (*) to search for multiple files that match a certain pattern.

Here are a few examples of how to use locate:

Search for a file by its exact name:

locate myfile.txt

Search for all files that contain the word “report”:

locate *report*

Search for all files with the “.pdf” extension:

locate *.pdf

Step 4 – Additional Options and Tips

By default, ‘locate’ searches are case-sensitive. If you want to perform a case-insensitive search, you can use the -i option:

locate -i myfile.txt

‘locate’ also supports the use of regular expressions for more complex search patterns. To use a regular expression, you can use the -r option:

locate -r '^myfile.*\.txt$'

If you expect a large number of search results, you can limit the output using the -n option:

locate -n 10 *report*

If you’ve created new files or made changes to your file system, you might need to manually update the ‘locate’ database to ensure accurate search results. You can do this by running the updatedb command again:

sudo updatedb

Troubleshooting

  • “locate command not found” error: If you encounter this error, it means the mlocate package is not installed on your system. Follow the steps in the “Install Locate Command” section to install it.
  • “sudo updatedb” permission denied: Make sure you’re running the updatedb command with sudo or as the root user.
  • Outdated search results: If ‘locate’ is returning outdated results, try manually updating the database using the sudo updatedb command.

The ‘locate’ command is a powerful tool for quickly finding files on your Linux system. By understanding how ‘locate’ works and following the steps outlined in this guide, you can efficiently manage your files and streamline your workflow. Remember to keep your ‘locate’ database up-to-date to ensure accurate search results.