Skip to content
Default image

Linux locate command

April 28, 2022

Working on Linux, searching files and directories will be one of the most common operations. To make our life easier the best way to do this is using the locate and find commands.

In this post I will write about the locate command.

locate

This command is the simplest way to search for files and directories by their names.

Based on the Linux distribution you are using, the locate package may not be pre-installed.

We can check if the utility is installed, by typing locate in the terminal and pressing enter.

As we can see, the Debian distro I am running doesn’t have the locate package installed. So what’s next?

Let’s install the mlocate package by running the following commands:

sudo apt update
sudo apt install mlocate

Let’s check again to see if the package is installed, type locate in the terminal and hit enter. We should see the following message locate: no pattern to search for specified.

At this point you might be wondering how the locate command works

locate will search for a pattern through a database file, generated by the updatedb command. When mlocate package is installed, a cron job is created that runs the updatedb command every 24 hours.

Please note that no result will be displayed if you search for a file/directory created after the update has been made. In order to avoid this you can manually update the database, running the updatedb command yourself as root or user with sudo privileges:

Now let’s see how to use the locate command (https://man7.org/linux/man-pages/man1/locate.1.html)

The locate command as the following syntax:

locate [option] [pattern]

At some point I’ve created a file named myfile but I forgot where it was created. The locate command will help me this time:

We can also use wildcards when using the locate commnad. If you need to find files with a certain extension:

Here are a couple of options to run with the locate command that will help you:

locate -c

-c option will print the number of matching entries instead of printing each finding

locate -n

-n [number] will limit the search results to the number following the -n option

locate -i

By default, the searches performed by locate are case-sensitive. In order to avoid this we use the -i (–ignore-case) option.

locate -e

If you haven’t updated the database, when running the locate command you will be shown the deleted files as well. In order to avoid this issue we need to use -e option

As you can se from the example above, I have deleted the test.txt file but i didn’t run the updatedb command so if I use the locate command without the -e option I still see the file.

Conclusion

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


In order to have accurate results, please keep in mind to run the updatedb command.

To see all the options that can be used with the locate command run man locate in the terminal or visit this link.