Linux locate Command: Syntax, Options, Examples

November 7, 2024

Introduction

The locate command is a Unix utility used to find files and directories quickly. The command is a more convenient and efficient alternative to the find command, which is aggressive and takes longer to complete the search.

Opposite to find, the locate command doesn't search the entire filesystem. Instead, it looks through a regularly updated file database in the system. Therefore, the search is completed much faster.

In this tutorial, you will learn to use the locate command in Linux.

Linux locate Command: Syntax, Options, Examples

Prerequisites

Installing locate Command

Depending on which Linux distribution you are using, the locate package may not be pre-installed.

To check if locate is installed, enter:

locate

The command prints the following output if the tool is already installed:

locate terminal output

In case the utility is not pre-installed, the output message is:

locate command not found terminal output

The following text explains how to install locate on Debian-based and Red Hat-based systems.

Debian-based Linux Distros

Follow the steps below to install locate on Debian-based systems:

1. Update the package repository:

sudo apt update 

2. Install the locate package by running:

sudo apt install plocate

RH Based Linux Distros

Follow the steps below to install locate on RH-based systems:

1. Update the package repository:

sudo yum -y update

2. Run the following command to install locate:

sudo yum install mlocate

Note: Installing the mlocate package creates a cron job that runs the updatedb command every 24 hours and updates the database. To manually update the database, use sudo updatedb.

Linux locate Command Syntax

The locate command syntax is:

locate [options] [pattern]

The locate command works without any options. However, you must provide a pattern that specifies what you want to search for.
A pattern is the search term or string used to find matching files or directory paths.

locate Command Options

The locate command works with several options. The following table presents common locate options:

OptionDescription
-0, --null Separates output entries using the ASCII NUL character instead of printing each entry on a new line.
-A, --all Displays only the entries matching all the specified pattern(s).
-b, --basename Matches only the base name (filename) against the specified patterns, ignoring the directory path.
-c, --count Outputs the number of matching entries instead of the filenames.
-d, --database Specifies an alternate database path as a colon-separated list of database files instead of the default database.
-e, --existing Only displays entries for files that currently exist on the file system.
-h, --help Displays help information with a list of all available options.
-i, --ignore-case Ignores case sensitivity when matching patterns.
-L, --follow Checks if files exist when used with --existing; omits broken symbolic links.
-l, --limit Limits the output to a specified number of results.
-n [LIMIT] Alternative to -l, specifies the maximum number of matching entries to display.
-p, --ignore-spaces Ignores punctuation and spaces when matching patterns.
-q, --quiet Suppresses error messages encountered while reading and processing databases.
-r, --regexp Uses a basic regular expression for searching. This option does not allow additional patterns as arguments.
-S, --statistics Outputs database statistics (like file count and size) instead of searching for files.
-t, --transliterate Treats letters with accents as if they were their unaccented versions.
-V, --version Displays the version information for locate.
-w, --wholename Matches the entire path name against the specified patterns, not just the base name.

Note: Some options, like -S and -q, are only available with mlocate, a version installed on Red Hat-based systems. Meanwhile, options such as -p, -t, and -A are exclusive to plocate, a newer version often found on Debian-based systems like Ubuntu.

Linux locate Command Examples

The locate command finds files in Linux using the filename. The command performs the search using a database containing bits of files with the corresponding paths in the system.

However, locate doesn't check for files outside the database, which means it doesn't report about files created after the most recent database update.

The following sections list helpful examples of using the locate command.

Search for a File

The simplest way to use the locate command is to input the pattern you want to search for.

For example, search for all files containing the mysql pattern in the filename by running the following command:

locate mysql
locate mysql terminal output

The output lists all the absolute paths for the files containing the mysql pattern in the filename.

Format the Output

When the search result is a long list of files, pipe the locate command to less for better readability and easier scrolling.

For example:

locate mysql | less
locate mysql | less terminal output

Navigate the output by pressing the Space bar to move forward, b to reverse, or Q to quit.

Show Existing Files

While the locate command retrieves results almost instantaneously, the speed comes with a cost. Sometimes deleted files appear in the results because the database that locate reads hasn't been updated since the files were deleted. Also, new files don't appear in the results if they were created after the last database update.

Eliminate these shortcomings using the -e option to search the filesystem and include only the existing files in the results.

For instance, delete a file using rm:

rm example.txt

The command has no output. Searching for the file with locate shows the deleted file's location:

locate example.txt
locate example.txt terminal output

However, when using the -e option, locate shows no results since the file was removed:

locate -e example.txt
locate -e example.txt terminal output

Note: To permanently delete a file and prevent its recovery, use the shred command.

Count Existing Files

Print the number of matched files instead of the filenames and their absolute paths by specifying the -c option.

For example:

locate -c mysql
locate -c mysql terminal output

The output contains only the number of files with mysql in the filename. All other information is suppressed.

Limit Number of Search Results

Limit the number of search results with the -n option to avoid redundant results. For example, limit the search to only ten results by entering:

locate mysql -n 10
locate mysql -n 10 terminal output

The output shows only the first ten files matching the search pattern.

Ignore Case Sensitive Files

By default, locate performs a case-sensitive search when processing the input query.

For instance, search example_file in lowercase.

locate example_file
locate example_file lowercase terminal output

locate shows no output because the filename contains uppercase letters. Searching for the file in uppercase shows the file's location:

locate EXAMPLE_FILE

locate EXAMPLE_FILE uppercase terminal output

Instruct locate to ignore case sensitivity using the -i option:

locate -i example_file
locate -i example_file terminal output

Search for File with Exact Name

By default, locate parses the input pattern as a string and shows all files containing the input pattern within the filename. For example, searching for mysql shows all files containing the mysql string as part of the filename, instead of files whose entire filename is mysql.

Search for a file with an exact name using the -r (regular expression) option.

For example:

locate -r /mysqld$
locate -r /mysqld$ terminal output

The output shows only the files whose filename completely matches the input pattern.

Note: The dollar sign ($) at the end of a regular expression marks the end of a line. The forward slash character (/) denotes regular expression boundaries.

Separate Output Entries with ASCII NUL

The output entries that locate produces are separated by a newline (\n) character. Use the -0 option to separate entries with ASCII NUL instead of the newline character.

For example:

locate -0 apache
locate-0-apache-terminal-output

The default separator is no longer the newline character, and the entries are separated with ASCII NUL.

Update locate Database

The database that locate uses automatically updates daily at the same time. To update the database manually and make sure the file index is correct, run the following command:

sudo updatedb

The update process takes a few seconds to complete, depending on the number of files on the system. The command produces no output.

Choose a Different mlocate Database

Instruct locate to use a database other than the default one by specifying the -d option. The feature is helpful when looking for results not present in the default mlocate database.

To specify a different database, use the following syntax:

locate -d [new database path] [pattern]

Ignore Diacritics and Accent Differences

The locate command uses ordinary letters to match results, which means that filenames containing letters with diacritics do not appear in the search results. Use the -t option to instruct locate to ignore such differences between letters and include diacritics in the results.

For example:

locate -t nice-cafe
locate -t nice-cafe terminal output

Searching for the nice-cafe pattern shows no results in the example above because the filename contains a diacritical mark. Searching for the same pattern again and specifying the -t option shows the nice-café file path as a result and ignores the diacritic in the filename.

Conclusion

This guide explained how to use the locate command to search for files in a filesystem quickly. While the utility has drawbacks, it is an excellent solution for finding files when speed is a priority.

For other useful Linux commands, check out our comprehensive Linux commands cheat sheet with a downloadable PDF document.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use the nslookup Command
January 13, 2022

This tutorial will introduce nslookup, a cross-platform command for querying servers and obtaining domain records.
Read more
How to Use the dmesg Linux Command
October 30, 2024

The dmesg utility allows you to inspect the kernel ring buffer and check for errors during system startup. This tutorial shows how to use the dmesg command.
Read more
How to Use the Linux head Command
January 5, 2022

The head command lists the first ten lines of a file in standard output. Learn how to use the head command and its options (with examples).
Read more
Using the Linux free Command
December 29, 2021

The free command in Linux is a utility that helps monitor RAM usage on a system. This guide shows how to use the free command and determine if there are enough resources for running new apps.
Read more