locate Command in Linux | Explained

The “locate” command in Linux is used to search for files on a system in a quick and easy way. It does that by searching through a database of file names rather than the file system itself.

This write-up will focus on two main parts, mentioned below:

Let’s start the discussion on the “locate” Command used in Linux.

Install the locate Command Support

The locate command doesn’t come pre-installed, and when you try to execute it on the terminal, you will get the following error:

$ locate testfile.txt

As you can see in the above image that it is showing an error that the command ‘locate’ not found and in the next line it is also telling you how to install it as well by typing:

$ sudo apt install plocate

The above command will only work for the Debian based operating system such as Ubuntu and Linux mint. If you are using CentOS, then you can type:

$ sudo yum install plocate

In case you are using Fedora, then you need to type:

$ sudo dnf install plocate

How to Use the locate Command?

After installing the locate command in your desired OS, the next thing is to understand its syntax to utilize it, which you can do by typing properly:

$ locate --help

Running this command will help you understand its syntax along with the options that you can use with this command.

Different Options Associated with the locate Command

In this section, we will discuss some of the most common ways to use this command for your better understanding.

Find all the Paths

If you want to list all the paths linked with any specific keyword, then you need to type:

$ locate nano

The above command displays all the path locations with the word “nano”.

Count Number of Search Entries Only

The “-c” option displays the count of the number of matched files rather than the names of the matched files. For example, if you want to know how many files on your system contain the word “nano,” you can use the following command:

$ locate -c nano

This will display the number of files that contain the word “nano”, but it will not display the names of the files. This can be useful if you just want a quick count of the files that match a certain search criterion rather than a detailed list of the files themselves.

Limit the Number of Search Results

The “-n” option is used to limit the number of results that are displayed. For example, if you want to find the first 10 files that contain the word “nano”, you can use the following command:

$ locate nano -n 10

This will display the names of the first 10 files that contain the word “nano”, but it will not display the names of any additional files that may also match the search criteria. This can be useful if you only want a limited number of results or just want to see the first few matches.

Ignoring the Case Sensitivity

The “-i” option is used to ignoring the case sensitivity, which means that the search will match files regardless of whatever case it can find. For example, to locate any files that include the term “readme,” regardless of case, you can type:

$ locate -i readme

This will display the names of all files that contain the word “readme”. regardless of whether the letters are written in uppercase, lowercase, or a combination of both. This can be useful if you are unsure how the search term is written or if you want to find all variations of the term.

Locating With the Exact Name

The “-r” option allows you to search for files using a regular expression, a type of pattern that can match complex strings of characters. For example, you can use the “-r” option to search for files that contain a specific pattern of letters or numbers or that match a specific naming convention.

$ locate -r /Documents

This command will exactly find the “/Documents” and will show you in the output wherever it finds out.

This is all about the usage of the locate command in Linux.

Conclusion

The “locate” command is a utility in Linux that is used to find the location of a file or directory on a system. It searches a database of file and directory names rather than the file system itself, which makes it much faster than other search tools, such as the “find” command.