Oct 19, 2023 5 min read

Locate Command in Linux

Use locate command in Linux with our step-by-step tutorial. The locate command uses a pre-built database to provide fast search results.

Locate Command in Linux
Table of Contents

Before we start talking about locate command in Linux, let's understand-What is locate command ?

The locate command is a useful utility in Linux that allows you to quickly search for files and directories based on their names. It uses a pre-built database to provide fast search results.

Searching for files and folders is one of the most frequent tasks when using Linux. There are several commands on Linux systems that allow you to search for files, with find and locate being the most used ones.

The easiest and fastest method for finding files and directories by name is to use the locate command.

In this tutorial, we will explain how to use the locate command. We will also address a few FAQs on locate command in Linux.

Installing locate (locate command not found)

The locate package may or may not be pre-installed on your Linux system, depending on the distribution and how the system was configured.

Open your terminal, type locate, and press Enter to see if the locate utility is installed. The system will display locate: no pattern to search for specified if the package is installed. Otherwise, you'll get a message like locate command not found.

If locate is not already installed, you can easily install it using your distro's package manager.

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 a database file generated by the updatedb command for a given pattern. The results of the search are displayed on the screen, one per line.

During the mlocate package installation, a cron job is created that runs the updatedb command every 24 hours. This ensures that the database is kept up to date. Check the /etc/cron.daily/mlocate file for more information about the cron job.

Manually updating the database is possible by running updatedb as root or a user with sudo privileges:

sudo updatedb

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

Files added after the database update will not appear in the find results.

Locate is much faster than the more powerful find command, which searches the file system, but it lacks many features and can only search by file name.

How to Use the locate Command

The syntax for the locate command is as follows:

Output

locate [OPTION] PATTERN...

When used without any options, the locate command will print the absolute path of all files and directories that match the search pattern and have read permission for the user.

For example, to look for a file called .bashrc, type:

locate .bashrc

All files with the string .bashrc in their names will be included in the output:

Output

/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 large, you can pipe it to the less command for easier readability:

locate .bashrc | less

The locate command accepts patterns involving globbing characters, such as the wildcard character *. The command checks for PATTERN when there are no globbing characters in the pattern.

As a result, all files containing the search pattern in their names were presented in the prior example. The wildcard character can represent 0 characters, 1 characters, or more characters. To search for all .md files on the system, for example, type:

locate *.md

To limit the search results, enter -n followed by the number of results you want to see. The following command will look for all .py files and display only the first ten results.

locate -n 10 *.py

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

locate -i readme.md
Output

```/home/vegastack/p1/readme.md
/home/vegastack/p2/README.md
/home/vegastack/p3/ReadMe.md

To display the count of all matching entries, use the -c (--count) option. The number of all files with the name .bashrc would be returned by the following command:

locate -c .bashrc
Output

6

Locate does not check whether the found files are still present on the file system by default. If you deleted a file after the most recent database update, the file will be included in the search results if it matches the search pattern.

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

locate -e *.json

If you need to perform a more complex search, use the -r (--regexp) option, which allows you to search using a simple regexp rather than patterns. This option can be specified more than once. For example, to search for all .mp4 and .avi files on your system while ignoring case, type:

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

FAQs for Locate Command in Linux

How do I use the locate command? 

Simply open a terminal and enter locate followed by the name of the file or directory you want to search for. For example, locate myfile.txt will search for myfile.txt.

Can I search for a partial name using the locate command? 

Yes, the locate command can search for partial names. For example, locate myfi will find files or directories containing the string 'myfi' in their names.

Does the locate command only search the current directory? 

No, the locate command searches the entire system, including all directories and subdirectories, to find matching files or directories.

Can I update the locatedb database manually? 

Yes, you can update the locatedb database by running the sudo updatedb command. This will refresh the database and make it aware of any recent changes to the file system.

Is the locate command case-sensitive? 

No, by default, the locate command is case-insensitive. However, you can use the -i option to perform a case-insensitive search.

Can I limit the search results with the locate command? 

Yes, you can use the locate command with options, such as -r for regular expression-based searches or -l to limit the number of search results displayed.

What if I don't get any search results with the locate command? 

If the locate command doesn't return any search results, it might be because the file hasn't been indexed yet. Update the locatedb database with sudo updatedb and try the search again.

Conclusion

The locate command searches the file system for files and directories with matching names. The command syntax is straightforward, and the results are displayed nearly immediately.

In your terminal, type man locate to learn more about the locate command's many options.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.