Linux locate Command

The locate command and find command is used to search a file by name. But, the difference between both commands is that locate command is a background process and searches the file in the database whereas, find command searches in the filesystem. The locate command is much faster than find command.

If you are unable to find a file with locate command, then it means that your database is out of date, and you can update your database with the "updatedb" command.

Syntax:

Options:

Some useful command line options are as following:

-A, --all: It is used to display only entries that match all PATTERNs instead of requiring only one of them to match.

-b, --basename: It is used to match only the base name against the specified patterns.

-c, --count: It is used for writing the number matching entries instead of writing file names on standard output.

-d, --database DBPATH: It is used to replace the default database with DBPATH.

-e, --existing: It is used to display only entries that refer to existing files during the command is executed.

-L, --follow: If the '--existing' option is specified, It is used for checking whether files exist and follow trailing symbolic links. It will omit the broken symbolic links to the output. This is the default behavior. The opposite behavior can be specified using the "--nofollow" option.

-h, --help: it is used to display the help documentation that contains a summary of the available options.

-i, --ignore-case: It is used to ignore case sensitivity of the specified patterns.

-p, --ignore-spaces: It is used to ignore punctuation and spaces when matching patterns.

-t, --transliterate: It is used to ignore accents using iconv transliteration when matching patterns.

-l, --limit, -n LIMIT: If this option is specified, the command exit successfully after finding LIMIT entries.

-m, --mmap: It is used to ignore the compatibility with BSD, and GNU locate.

-0, --null: It is used to separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line.

-S, --statistics: It is used to write statistics about each read database to standard output instead of searching for files.

-r, --regexp REGEXP: It is used for searching a basic regexp REGEXP.

--regex: It is used to describe all PATTERNs as extended regular expressions.

-V, --version: It is used to display the version and license information.

-w, --wholename: It is used for matching only the whole path name in specified patterns.

Examples of the locate Command

Let's see the following examples of the locate command:

How to use the locate command

Locate command is a useful utility for searching the files. It is pretty straight forward to use, just execute the command as follows:

Consider the below output:

Linux Locate

We can limit the search results to avoid redundancy by using the "-n" option.

For example. To display the just 5 results from our queries, execute the command as follows:

The above command will display the first five text files. Consider the below output:

Linux Locate

Display The Number of Matching Entries

To display the sum of matching files, execute the command with the '-c' option. Consider the below command:

The above command will display the sum of files having 'Demo' in their name. Consider the below output:

Linux Locate

Ignoring the case sensitivity

As we know, the Linux terminal is case sensitive. So, if we search a file in uppercase, it will only list the files in the upper case. To ignore the case sensitivity, execute the command with the '-i' option as follows:

The above command will display both the files having 'demo' and 'Demo' in their name. Consider the below output:

Linux Locate

Updating the mlocate Database

The locate command depends upon the 'mlocate' database. So, if the locate command is not working properly, we need to update the database. To update the database, execute the 'updatedb' command as follows:

Display Only Available Files in Our System

Sometimes, the locate command results deleted files. To avoid seeing the results for the deleted files, execute the command with the '-e' option. It will only display the files that are physically available in our system. Consider the below command:

It will produce the below output:

Linux Locate

Track status of the mlocate Database

To locate the database statistics, execute the command with the '-S' option as follows:

Consider the below output:

Linux Locate

Separate Output Entries with a Different Separator

The default output separator of the locate command is a newline. But we can change the default separator, For example, To use the ASCII NUL, execute the command using the '-0' option. Consider the below command:

It will display the output as:

Linux Locate
Next TopicLinux Date




Latest Courses