Linux has different tools to search and find files and directories. locate is one of them which is very popular. The alternative for locate is find command. But they work differently from each other. Find command searches in real time but locate command uses a database which holds all files and directories of the system. In this tutorial, we will look at how to use locate.

locate Command Syntax

locate [OPTION]... PATTERN...

locate Command Help

We can list help information about the locate command with the -h option.

$ locate -h

Help

Update locate Database

As we stated in the introduction locate command uses a database to hold whole file system directories and files. This database is not updated automatically. So we should update the database to use more effectively. As there are some files requires root privileges to read and update database we should provide root privileges for the updatedb command.

$ sudo updatedb

Generally, this update operation is not taking too much time in periodic updated. But if the update runs for the first time this may some more times to setup database.

Search With Locate

Now we have updated our database we can search for some terms. We will use locate command the provide the search term. In the example, we will search for a.txt term.

$ locate a.txt

Search

As we can see from results the term a.txt is not search for files it is simply looked in the full path string.

Display Only Existing Results

As we know locate command looks the database to find files and folders about the search term. What if the database is updated one year ago and there is a result those not exist anymore. Locate command has the ability to double-check if the result is existing currently. We will use -e option to activate this feature.

$ locate -e a.txt

As we can see the first result /home/ismail/a.txt not listed in the results because it is removed in the file system but the related record exists in the database. With -e option we have corrected the result.

Limit Results

Some times there may be a lot of results which will fill our terminal. This is generally an unwanted situation. There is an option that will limit the results count that will be printed to the terminal. We will use -l option with the number. In the example, we only want to list 5 results about search.

$ locate -l 5 a.txt

Display Result Count

Display Only Result Count

Some times we only interested in the count of results. This can be done in various ways with helper commands. But locate command all ready provides an option for this. We will use -c option to only list the count of results.

$ locate a.txt | wc -l

Display Only Result Count

Display Statistics and Database Information

The database of locate command will hold a lot of records. We can get information about this like statistics with -S option. this will list directories count, file count, size of file names, and the total size of the database.

$ locate -S

Display Statistics and Database Information

Display locate Command Version

The version of locate command can be displayed with --version option like below.

$ locate --version