One of the most common operations when working on Linux is to search for files and directories. On Linux systems, there are several commands which allow you to search for files with find and locate being the most used ones.

The locate command is the quickest and simplest way to search for files and directories by their names.

In this tutorial, we will explain how to use the locate command.

Installing locate (locate command not found)

Depending on the distribution and on how the system was provisioned the locate package may or may not be pre-installed on your Linux system.

To check whether the locate utility is installed on your system, open up your terminal, type locate, and press Enter. If the package is installed the system will display locate: no pattern to search for specified, otherwise you will see something like locate command not found.

If locate is not installed you can easily install it using the package manager of your distro.

Install locate on Ubuntu and Debian

sudo apt update sudo apt install mlocate

Install locate on CentOS and Fedora

sudo yum install mlocate

How Does locate Work

The locate command searches for a given pattern through a database file that is generated by the updatedb command. The found results are displayed on the screen, one per line.

During the installation of the mlocate package, a cron job is created that runs the updatedb command every 24 hours. This ensures the database is regularly updated. For more information about the cron job check the /etc/cron.daily/mlocate file.

The database can be manually updated by running updatedb command as root or user with sudo privileges:

sudo updatedb

The update process will take some time, depending on the number of files and directories and the speed of your system.

Files created after the database update will not be shown in the locate results.

Compared to the more powerful find command that searches the file system, locate operates much faster but lacks many features and can search only by the file name.

How to Use the locate Command

The syntax for the locate command is as follows:

locate [OPTION] PATTERN...

In its most basic form, when used without any options the locate command will print the absolute path of all files and directories that matches the search pattern and for which the user has read permission.

For example to search for a file named .bashrc you would type:

locate .bashrc

The output will include the names all files containing the string .bashrc in their names:

/etc/bash.bashrc
/etc/skel/.bashrc
/home/linuxize/.bashrc
/usr/share/base-files/dot.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/bash.bashrc
/usr/share/doc/adduser/examples/adduser.local.conf.examples/skel/dot.bashrc

The /root/.bashrc file will not be shown because we ran the command as a normal user that doesn’t have access permissions to the /root directory.

If the result list is long, for better readability you can pipe the output to the less command:

locate .bashrc | less

The locate command also accepts patterns containing globbing characters such as the wildcard character *. When the pattern contains no globbing characters the command searches for *PATTERN*, that’s why in the previous example all files containing the search pattern in their names were displayed.

The wildcard is a symbol used to represent zero, one or more characters. For example, to search for all .md files on the system you would use:

locate *.md

To limit the search results use the -n option followed by the number of results you want to be displayed. For example, the following command will search for all .py files and display only 10 results:

locate -n 10 *.py

By default, locate performs case-sensitive searches. The -i (--ignore-case) option tels locate to ignore case and run case-insensitive search.

locate -i readme.md
/home/linuxize/p1/readme.md
/home/linuxize/p2/README.md
/home/linuxize/p3/ReadMe.md

To display the count of all matching entries, use the -c (--count) option. The following command would return the number of all files containing .bashrc in their names:

locate -c .bashrc
6

By default, locate doesn’t check whether the found files still exist on the file system. If you deleted a file after the latest database update if the file matches the search pattern it will be included in the search results.

To display only the names of the files that exist at the time locate is run use the -e (--existing) option. For example, the following would return only the existing .json files:

locate -e *.json

If you need to run a more complex search you can use the -r (--regexp) option which allows you to search using a basic regexp instead of patterns. This option can be specified multiple times.

For example, to search for all .mp4 and .avi files on your system and ignore case you would run:

locate --regex -i "(.mp4|.avi)"

Conclusion

The locate command searches the file system for files and directories whose name matches a given pattern. The command syntax is easy to remember and results are shown almost instantly.

For more information about all available options of the locate command type man locate in your terminal.

If you have any questions, feel free to leave a comment.