The “locate” Command in Linux [7 Practical Examples]

The locate command is very useful for searching files in the background on an existing database of Linux. You will also be able to know the location of the files with it. In this article, I will describe all the features and uses of the locate command in Linux with multiple examples.

A. Description

The locate command performs the search operation from an existing database and prints the results with the exact directory path.

B. Syntax

The syntax for the locate command is pretty simple which is the command itself followed by some specific options and the pattern you want to locate.

locate [OPTION]... PATTERN...
Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. Moreover, 3 dots after PATTERN suggests that you can specify multiple matching patterns for the files.

C. Options

Many useful options are available for the locate command. I have listed some of the most used ones here. However, if you want to know more about options for the locate command you can always check the man page for the locate command.

man locate

Useful Options

  • -c, –count (Does not print any matches but rather the total occurring number)
  • -e, –existing (Only prints the existing match)
  • -i, –ignore-case (Ignores the sensitivity of cases)
  • -p, –nofollow (Ignores punctuation and spaces when  matching  patterns)
  • -l,N, –limit=N (Stop searching after limit matches have been found.)
NB: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

How to Install the “locate” Command in Linux

Most of the Linux distributions have the locate package preinstalled. To check whether you have it preinstalled or not just type the below command on the command prompt:

locate

If you don’t have it preinstalled, it will show the below output:locate command not installed in LinuxTo install the locate package, you can follow the below instructions.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

sudo apt install mlocate

➌ Type your password.

➍ Now, press the ENTER button.

After completing the entire process, locate will be installed on your machine as shown in the below picture.installing locate command in Linux

Practical Examples of the “locate” Command in Linux

The locate command in Linux is an easy-to-use command that is used to find the location of files from an existing database. Some of the most useful applications of the locate command have been illustrated below.

Example 1: Search for a File Using the “locate” Command in Linux

You can search for a file using the locate command. To search any file you should follow the below procedure. Here, I have searched for a file named mysql.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

locate mysql

➌ Press the ENTER button.

Output >

You can see in the below picture that all the files named mysql are listed on display.searching a file using locate command in Linux


Similar Readings


Example 2: Limit the Number of Search Results

You can limit the number of search results using the locate command. To do so you should follow the below procedure. Here, I have limited the number of search results for html.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

locate html -n 5

➌ Press the ENTER button.

Output >

You can see in the below picture that only 5 html files are listed on display.Limit the Number of Search Results

Example 3: Format the Output Using the “locate” Command in Linux

Sometimes there are huge amounts of search results appear for the locate command. You can format the output for your convenience using the locate command. Here, I have formatted the output of search results for the bash files. To do so you should follow the below procedure.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

locate bash | less

➌ Press the ENTER button.

Output >

You can see in the below picture that all the bash files are listed on display in a page-by-page format. You can move to the next page by pressing the SPACE BAR and to the previous page by pressing the B button. You can exit from the list by pressing the Q button.Format the Output page by page

Example 4: Show the Existing Files Using the “locate” Command in Linux

After removing some files, you will still see the non-existing files in the display using the locate command. You can skip it by adding the -e option with the locate command. Here, I have deleted my file2.txt file and shown it with the locate command. To do so you can follow the below procedure:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

rm file2.txt

➌ Press the ENTER button.

This will delete the file from the machine.

➍ Type the following command in the command prompt:

locate file2.txt

➎ Press the ENTER button again.

You will see that the locate command still shows the file location.

➏ Type the following command in the command prompt:

locate -e file2.txt

➐ Finally, press the ENTER button.

Output >

You can see in the below picture that the file2.txt file’s location is not shown this time.Show the Existing Files Using the “locate” Command in Linux

Example 5: Count the Existing Files Using the “locate” Command in Linux

You can count the number of existing files using the locate command. Here, I have counted all my html files. To do so you can follow the below procedure:

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

locate -c html

➌ Press the ENTER button.

Output >

You can see in the below picture the total number of html files printed here.Count the Existing Files

Example 6: Ignore Case-Sensitivity

The locate command by default doesn’t show the files if the name’s cases are wrong. To ignore the case sensitivity you should follow the below procedure. Here, I have used the file3.txt file to show you the example.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

locate FILE3.txt

➌ Press the ENTER button.

This will not show any file as the case is not correct for the filename.

➍ Type the following command in the command prompt:

locate file3.txt

➎ Press the ENTER button again.

You will see that the locate command will show the file location.

➏ Type the following command in the command prompt:

locate -i FILE3.txt

➐ Finally, press the ENTER button.

Output >

You can see in the below picture that the file3.txt file’s location is shown this time, although I did not put the correct cases for the filename.Ignoring Case-Sensitivity


Similar Readings


Example 7: Display Files by Their Extensions Using the “locate” Command in Linux

You can search for a specific file type using the locate command. To do so you should follow the below procedure. I have searched for all of my files containing .txt extensions at the end of their name.

Steps to Follow >

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

locate “*.txt”

➌ Press the ENTER button.

Output >

You can see in the below picture that all the filenames containing .txt extensions at the end of their name are listed on display. Display Files by Their Extensions Using the “locate” Command in Linux

Conclusion

After completing this article, you will acquire all the knowledge about the locate command in Linux. With the given practical examples, you will be able to execute many commands to locate the location of different files on your machine.


Similar Readings

Rate this post
Borhan Uddin

Hello, I am Borhan Uddin, a resident of Dhaka, Bangladesh. I have completed my undergraduate degree in Electrical and Electronic Engineering (EEE) from Chittagong University of Engineering and Technology (CUET). I love to spend my leisure by playing and watching various movies & dramas. Currently, I am working as a Linux Content Developer Executive here. I would like to learn more about Linux every day of the week and would be keen to share it with you rapidly. Read Full Bio

Leave a Comment