Linux locate command with Examples

Written by: Bobbin Zachariah   |   Last updated: December 12, 2023

The locate command in Linux is a powerful tool for quickly searching and locating files and directories on your system by filename. Unlike some other search methods, locate works by searching a pre-built database, making it incredibly much faster than other search methods like using the find command.

Syntax

The basic syntax of locate command as follows

locate [options] [pattern]

Some common options you can use are:

  • -i, --ignore-case: Perform a case-insensitive search.
  • -e, --existing: Displaying only files that currently exist on the system.
  • -E, --non-existing: Display only names that currently do not exist.
  • --max-database-age D: Change the warning threshold for old databases.
  • -p, --print: Print search results even when using --statistics or --count.
  • -r, --regex: Speciy regular expression dialect.
  • -l N, --limit=N: Limit the number of matches to N.
  • -w, --wholename: Match against the full name of the file as listed in the database.
  • -c, --count: Display the total count of matches found.
  • -d path, --database=path: Search specific database files in a colon-separated list.
  • -0, --null: Use ASCII NUL as a separator instead of a newline.

Installation

Generally, the locate tool doesn't come preinstalled in most Linux Distributions. It's part of mlocate package and you can install easily using the package manager of the specific Distro.

If you are getting 'locate' not found on running the command, it needs to be installed.

Pick your Distro and choose the appropriate command to install locate tool:

Ubuntu / Debian
sudo apt install mlocate
Redhat
sudo yum install mlocate
Fedora
sudo dnf install mlocate
Arch Linux
sudo pacman -S mlocate

During the installation, an initial database is created. This is the heart of locate command and it stores information about the paths of files and directories on the system.

Note: Many Linux distributions are in transition to plocate as a faster and more efficient alternative to mlocate. plocate offers several advantages, including increased speed and reduced disk space usage. The fact that some distributions like Fedora and Debian have already adopted plocate as the default implementation highlights its potential benefits.

Update locate Database

Keeping the locate database up to date is crucial for accurate search results. This database is usually updated using a cron job (/etc/cron.daily) that runs the updatedb command (plocate on many latest version). The updatedb command scans the file system, records the names and locations of all files and directories, and then stores this information in the database. By default, it updates the database daily.

To manually update the database, run:

sudo updatedb

Keep in mind that updating the database manually may take some time, depending on the size of your filesystem and the number of files.

Examples

Let us check how to use locate command with examples

Search files by name

For example, to locate all files and directories related to "mysql," use:

$ locate mysql
search files by name

So when you run the locate command with the keyword "mysql," it searches for all files and directories containing "mysql" in their names or paths on your Linux system.

Note: here locate will behave as if you entered *mysql*, and it will search for any file or directory names containing "mysql" anywhere in their names.

If the search results are extensive you can combine locate and less using pipe to display one page at a time:

$ locate update | less

In case you want to search multiple filenames, you need to call locate command multiple times. Check the following 2 examples:

$ locate -r 'mysql' | locate -r 'update'
$ (locate mysql ; locate update)

You can also search for file names and filter by keywords by combining them with other commands such as grep. Examples:

$ locate txt | grep '/home/ubuntu' | grep -v '/\.'
$ locate -0 /var/log | grep 'error'

Note: With the -0 option, the output will be NULL-terminated, and each match will be separated by a NULL character instead of a newline character.

Case-Insensitive Search

To perform a case-insensitive search using the -i option.

$ locate Mysql
$ locate -i Mysql
search files case insensitively using -i option

The first command, locate Mysql, did not yield any results. This is because locate is case-sensitive, and it was searching for files and directories with the exact case "Mysql."

Limit the number of search results

To limit the number of results displayed, use the -l option followed by the desired count:

$ locate -l 5 mysql
limit the number of result displayed using -l

This will search for files and directories containing the word "mysql" in their names and display the first 5 search results.

Count the number of matches

To count the number of matches, you can use the -c option.

Example:

$ locate -c mysql
23

The output shows a count of 23 matches for the keyword "mysql" in the locate database on your system.

Match the search pattern against base name

To match the search pattern against the base name (the final component) of file names, you can use the -b or --basename option.

Example:

$ locate -b mysql
match against base name

This command will search for files and directories where "mysql" is the base name (the final component) of their names.

Using Regex

When you use --regex, you can provide a regular expression pattern that specifies the search criteria for locating files and directories.

Example 1: locate --regex "(\.rar|\.zip)"

This searches for files and directories whose names match the regular expression pattern (\.rar|\.zip)

Example 2: locate -r mysql$

Searches for paths (file or directory names) that end with "mysql".

Example 3: locate --regex "file*"

This searches for files and directories on your system whose names match the regular expression pattern "file*," where "file" appears at the beginning of the names followed by zero or more characters.

Show Only Existing Files

If you haven't updated the database, the locate command may even display a deleted file. The -e option allows you to display only the names of files that currently exist on your system.

Example:

locate -e mysql-data
display only existing files using -e option

This option is useful to avoid displaying files that have been deleted or no longer exist in the file system but are still listed in the locate database.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master’s degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification and RedHat Enable Sysadmin.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment