The Linux locate command is used to search files located on the user’s machine or on the server. In fact, she performs the same work as the find command, however, is searching in its own database. Find the step-by-step through the entire hierarchy of directories.
Update the locate database automatically, usually once a day. Set a desired interval between scans of the system, you can use task scheduler Cron.
- The syntax and options of locate
- Examples of how to use locate
The syntax and options of locate
In the terminal emulator the locate command is written in this format:
locate options sablonok
- -A (–all) — displays only files that match all specified patterns.
- -b (–basename) — removes a file if its file name matches the pattern.
- -c (–count) — instead of addresses displays the number of files found.
- -d (–database) — change a database to search, default to custom.
- -e (–existing) — search files existing at the time of the launch team.
- -L (–follow) — when using the –existing causes the command to use symbolic links instead of hard. Option –follow enabled by default.
- -h (–help) — displays help.
- -i (–ignore-case) — makes the command case insensitive.
- -p (–ignore-spaces) — makes the command ignore spaces and punctuation.
- -t (–transliterate) — ignores diacritical marks.
- -l, -n (–limit) — limits the number of files in the command output.
- -P (–nofollow) — causes the command to use hard links instead of symbolic.
- -0 (–null) — involves the use NUL as a line separator.
- -S (–statistics) — displays statistics for each database instead of file search.
- -q (–quiet) — forbids the command to display error messages until the search is completed.
- -r (–regexp) — search using regular expressions.
- -V (–version) — shows the version installed on the system utility locate.
- -w (–wholename) — removes the file if its name or address has characters that match the pattern.
Examples of how to use locate
If you want to find files that match multiple patterns, use the option -A. Templates are separated by a space:
locate-A zima 64
Option -w enabled by default, so locate checks for pattern match, not only file names but also the names of the folders where these files are located. For example, if Document1.ods is located at Computer/Buhalteriams/Seadocument/ it will be displayed by the command when using templates, “Doc”, “Buch” and “All”. In order for the results to show only files with names that contain the pattern, use the option -b.
The screenshot below shows how the team handles the queries with each of the options.
The command locate is case sensitive. If, for example, the pattern is “GrEEn”, files with names green and green in the search process will be ignored. In order to find all documents, regardless of uppercase and lowercase letters in their names, use the option -i.
Sometimes file names are letters with diacritical marks (umlaut, acutane, tremani and others). These documents are not found if the command uses a template that consists of ordinary letters. Conversely, if the template contains letters with diacritical marks, the command will ignore files that have names that do not have the same characters. “Erase” the difference between letters with diacritics and without them, you can use the option -t.
By default, the command locate searches in a private mlocate database.db, however, is able to work with a custom database. This provides the following option: —database.
locate --database main-user-tor-database1.db bin/raw
You can simultaneously search in multiple databases, thus file names of these bases need to be separated by a colon:
locate --database main-user-tor-database1.db:main-user-tor-database2.db bin/raw
The results are displayed separately for each database in the same order in which the databases were listed in the request.
Files added to the system to update the standard database, not found command locate. Also, the results may appear non-existent (deleted) documents. In order to update the results, use the option -e.
Looking at the screenshot, it is easy to compare the search results with the option -e without it.
In addition, you can update the database manually by running the command in the terminal:
The locate command linux allows you to quickly and easily find the files lost in the bowels of the system. If some aspects of its work were not clear to you, ask questions in the comments.