locate command in Linux

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

We use the locate command to find files in Linux, unlike the find command that searches the entire file system, this command uses a local database to search for the file passed as an argument.

Table of contents.

  1. Introduction.
  2. Installation.
  3. Syntax.
  4. Commands.
  5. Summary.
  6. References.

Introduction.

In Linux, we use the locate command to find files bypassing their names as arguments to the command.

Another Linux utility for finding files and directories in Linux is the find command.

The difference between these two utilities is that, locate command searches the database(mlocate database) and reports the location of the file we are searching for. The find command traverses all directories and sub-directories searching for a file that matches the criteria.

Installation.

If the locate command is not installed in the system, we can install it as follows:
Ubuntu and Debian Systems.

$ sudo apt install mlocate

CentOS/RHEL Systems:

$ sudo dnf install mlocate

The locate command searches through a pre-built database that has all files on the file system.
For the locate command to find a file, the file needs to be in the mlocate database, updates to this database are not rapid and therefore we use the updatedb command to immediately update it. For example, let us create a file and try to locate it:

$ touch file.txt
$ locate file.txt
$ sudo updatedb
$ locate file.txt

From the session above, we first create a file and try to locate it, however, this file has not yet been added to the database so we execute the updatedb command which performs this action. Now when we try to locate the newly created file, we find it.

Syntax.

We write locate commands using the following format:

$ locate [OPTION] [PATTERN]

Commands.

A basic locate command involves the locate command followed by the file name executed without any options:

$ locate file.txt

Sometimes we perform a generalized search, for example, searching for file extensions.

To search for all .txt files we pass .txt as an argument to the locate command, however, the results might be very many and maybe we only need a specific number. For this, we use the -n option followed by the number of search queries we need to be returned.

Let's get the first 5 results from searching all binaries files:

$ locate bin -n 5


As an alternative, we can also use the less command with its options to limit output.

To get the count of all these files without actually printing them out we use the -c option as follows:

$ locate -c bin

By default, the locate command is case sensitive, meaning, a file called file.txt is very different from a file called File.txt because of the case of f. To ignore case during a search, we use the -i option:

$ locate -i file.txt

Usually, when we update the database, the command will still return files even if their physical copies have been removed. This is not desirable, to eliminate such files we use the -e option.

$ locate -e [fileName]

Here we print files that currently exist in the database instead of those that previously existed.

The default separator for the output is a new line, that is all files will be printed on a new line. Some situations may require us to print this output with different separators for example ASCII NUL. For this we use the -0 option:

$ locate -i -0 [fileName]

To view the status of the local database we execute the locate command which is followed by the -S option:

$ locate -S

We can have multiple databases and if this is the case we need to tell locate command where to search for files. In this case, we can specify the database where locate will search as follows:

$ locate -d [new db] file

Here we have specified new db as the other database other than the default where locate should look for files.

While searching for files, we may encounter errors, we can suppress them using the quiet option -q as follows:

$ locate -q file.txt

Some files have diacritic and accent differences and when we try to locate them, they are not returned. To ignore such hindrances, we use the -t option:

$ locate -t [file.txt]

The default behavior of the locate command is to return all files with the pattern we are searching for. For example, when we search for apache files, all files with the string apache will be returned.

To run complex searches where we try to get the exact filename, we use regular expressions, with the locate command this is the -r option.

$ locate -r "[regex]"

For example, to find all files ending in /apache2 we execute:

Notice that we use the $ that matches the end of a string.

Summary.

In Linux, we use the find, locate and which commands to search for files. All three search for files but their outputs differ.

The locate command is faster since it searches a database instead of the entire file system. A con of locate command is that files recently added cannot be found since the database is not updated rapidly. Also, not all files are stored in the database.
The find command is used to search for a file in the file system, it is not as fast but is effective. Also, but limiting the search space, we can improve its speed.

References.

For a comprehensive guide to locate command, we can execute the $ man locate command and read its command manual.
find command

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.