The locate Linux command is used to find files located on the user’s machine or server. In fact, it does the same job as the find command, however, it searches its own database. Find, on the other hand, goes through the entire directory hierarchy step by step.

The locate database is updated automatically, usually once a day. You can set the desired interval between system scans using the Cron task scheduler.

Syntax and options for locate

In a terminal emulator, the locate command is written in this format:

locate options search_pattern

Options:

  • -A (–all) – displays only files that match all given patterns.
  • -b (–basename) Outputs the file if its name matches the pattern.
  • -c (–count) – displays the number of found files instead of addresses.
  • -d (–database) Changes the default search database to a custom one.
  • -e (–existing) – search for files that exist at the time the command is run.
  • -L (–follow) – when applied –existing causes the command to use symbolic links instead of hard ones. The –follow option is enabled by default.
  • -h (–help) Displays help.
  • -i (–ignore-case) – makes the command case insensitive.
  • -p (–ignore-spaces) causes the command to ignore spaces and punctuation.
  • -t (–transliterate) ignores diacritics.
  • -l, -n (–limit) – limits the number of files in the command output.
  • -P (–nofollow) – forces the command to use hard links instead of symbolic ones.
  • -0 (–null) – Enables the use of NUL as a line separator.
  • -S (–statistics) – displays statistics for each database instead of searching for files.
  • -q (–quiet) Prevents the command from printing error messages until the search is complete.
  • -r (–regexp) – search using regular expressions.
  • -V (–version) Shows the version of the locate utility installed on the system .
  • -w (–wholename) – displays the file if its name or address contains characters that match the pattern.

Locate Usage Examples

If you need to find files that match several patterns at once, use the -A option . Patterns are separated by a space:

locate -A zima 64

The -w option is enabled by default, so locate matches the pattern not only with filenames, but also with the names of the folders in which those files are located. For example, if Document1.ods is located at Computer/AccountingDocs/AllDocuments/, it will be displayed by the command when using the templates “Doc”, “Buch” and “All”. To ensure that only files with names that contain the pattern are displayed in the results, the -b option is used .

For comparison:

locate -b zima

locate -w zima

locate command in Linux

The screenshot shows how the team processes requests with each of the mentioned options.

The locate command is case sensitive. If, for example, the template is “GrEEn”, files named green and GREEN will be ignored during the search. To find all documents, regardless of the presence of lowercase and uppercase letters in their names, use the -i option .

Sometimes in file names there are letters with diacritical marks (umlauts, acutes, trems, and others). These documents will not be found if the command uses a pattern consisting of regular letters. Conversely, if the pattern contains letters with diacritical marks, the command will ignore files that do not have similar characters in their names. You can “erase” the difference between letters with and without diacritics using the -t option .

locate command in Linux

By default, the locate command searches its own database, mlocate.db, but it can also work with custom databases. The following option is provided for this: database .

locate --database main-user-tor-database1.db bin/raw

You can simultaneously search in several databases, while the file names of these databases must be separated by a colon:

locate --database main-user-tor-database1.db:main-user-tor-database2.db bin/raw

The results will be displayed separately for each database, in the same order in which the databases were listed in the query.

locate command in Linux

Files added to the system before the standard database was updated are not found by the locate command . Also, non-existent (deleted) documents may appear in the results. To update the output, use the -e option .

Looking at the screenshot, it’s easy to compare search results with and without the -e option.

locate command in Linux

Alternatively, you can update the database manually by running the following command in a terminal:

sudo updatedb

locate command in Linux

Findings

The locate linux command allows you to quickly and easily find files lost in the bowels of the system. If any aspects of her work remain unclear to you, ask questions in the comments.

Source: https://losst.ru/. The article is distributed under the CC-BY-SA license