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.
Prerequisites
- A system running Linux.
- Access to the terminal.
- An account with sudo or root privileges.
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:
In case the utility is not pre-installed, the output message is:
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:
Option | Description |
---|---|
-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
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
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
However, when using the -e
option, locate
shows no results since the file was removed:
locate -e example.txt
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
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
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
shows no output because the filename contains uppercase letters. Searching for the file in uppercase shows the file's location:
locate EXAMPLE_FILE
Instruct locate
to ignore case sensitivity using the -i
option:
locate -i example_file
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$
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
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
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.