How to Use the locate Command in Linux in 5 ways

linux-locate-command

You can use two commands in Linux to locate/find files. You can use the find command or the locate command. The choice of which to use will depend on your search intent.

The locate command is handy for quick searches as it only checks for the file in the database. However, the find command does a comprehensive search for the file in all the directories in your system. The find command is more detailed in its search, but the locate command only checks the details on the database, and if the database needs to be updated, it may fail to capture recently added files.

This tutorial guides you on how to use the locate command to find files. We will also cover different locate command examples for a better understanding.

Syntax of the Linux ‘locate’ Command

The locate command follows the below syntax.

The locate command will return an exit status of 0 if the search pattern is matched or an exit status of 1 if no match is found.

How to Install the ‘locate’ Package

Unlike the find command, the locate command comes as a package that must be installed before you can use it.

Running the locate command will confirm whether the package has already been installed.

verify-locate-package

If not installed, you must install the locate package. For Ubuntu and Debian users, run the command below.

sudo-apt-install-plocate

For CentOS and Fedora:

Verify that the locate package is installed successfully by checking its version.

We have plocate 1.1.15 installed for our case.

check-locate-package-version

Examples of Using the Linux ‘locate’ Command

Having discussed the locate command and how to install its package, the next task is to go through different examples to understand how to use it.

Example 1: Search for a File Using Its Name

The primary way to use the locate command is by adding the file name you want to find. It will then search for that file in the database and give the paths for results matching the search.

Here’s an example.

locate-file-using-name

Example 2: Set Limit for Search Results

Sometimes, your search result after running the Linux locate command can yield numerous results. You can specify the limits to which the results should be displayed.

For instance, if we only want to see three results, we would use the -n flag as follows.

locate-command-set-output-limit

Example 3: Show Count of the Matches Results

Instead of displaying the paths for all the matched files, you can decide only to get the count of all the matching files.

The -c option will give the count and avoid showing all the results.

The output below shows that we have 11 files that match our locate pattern.

locate-command-show-count

Example 4: Ignore Cases When Using the locate Command

The locate command is case-sensitive, for instance, if you have two files sharing the same name but in different cases. You won’t get them both in the results unless you specify to ignore the cases.

The -i option allows the locate command to show all matching files regardless of their cases. The example below matches ‘List.txt’ and ‘list.txt’, although our search term is ‘list.txt.’

locate-command-ignore-cases

Example 5: Redirect Output of the ‘locate’ Command

Even when running the command, it’s possible to save the results of the command in a file using the redirect sign (>). This method is ideal when you want to avoid displaying the results in the standard output and want to use them later.

Once you’ve redirected the results, you can verify that your new file has been created using the ls command. We’ve coupled it with the grep command to filter the results.

locate-command-with-grep

Lastly, open the file to check its contents. We’ve used the cat command to confirm that our file shows all the matched results of our locate command.

verify-redirect

Example 6: Update the ‘locate’ Database

We mentioned that the locate command relies on the database to search for a file. Although the update updates automatically, as for the case of CentOS, you can manually update it to get better results.

Run the command below.

update-locate-database

Conclusion

The Linux locate command is handy in finding files and offers a quick alternative to using the find command. The command uses a database to search for files, and you may sometimes get errors, especially if you still need to update the database, as locate may fail to capture the recently added files.

We’ve discussed the locate command and given examples of how to use it in Linux. Have fun searching for files.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top