The locate command of one Linux command per day

Time:2021-5-9

Locate allows users to quickly search whether there are specified files in the file system. The method is to establish a database including all the file names and paths in the system, and then when searching, you only need to query the database, instead of actually going deep into the file system. In general distribution, the establishment of database is automatically executed in crontab.

1. Command format:

Locate [select parameter] [style]

2. Command function:

The locate command can quickly find files when searching the database. The database is updated by the updatedb program. The updatedb program is established periodically by cron daemon. The locate command is faster than the whole hard disk data when searching the database. But the worse thing is that if the files found by locate are newly established or just renamed, they may not be found. In the internal setting, the, Updatedb runs once a day and can be updated by modifying crontab( etc/crontab)

Locate specifies that it is used to search for qualified files. It will go to the database where file and directory names are stored to find files or directory records that meet the template style conditions. Special characters (such as “*” or “?” can be used For example, if the template is kcpa * ner, locate will find out all the files or directories whose start string is kcpa and end string is ner, such as kcpartner, if the directory name is kcpa_ Ner will list all files in the directory, including subdirectories.

The function of locate command is similar to that of find. However, locate creates an index database for all files and directory data in the hard disk through the update program. When the loade is executed, the query speed will be faster. The index database is generally managed by the operating system, but the update can also be issued directly to force the system to modify the index database immediately.

3. Command parameters:

-E will be excluded from the search.

-1. If it is 1. Start safe mode. In safe mode, users will not see files that cannot be seen by permissions. This slows down because locate has to go to the actual file system to get the file’s permission data.

-F exclude the specific file system, for example, we do not have the reason to put the files in the proc file system in the database.

-Q quiet mode, no error message will be displayed.

-N displays at most N outputs.

-R use the normal expression to find the condition.

-O specify the name of the data stock.

-D specify the path to the database

-H display auxiliary message

-V displays the version information of the program

4. Application examples:

Example 1: find all files related to PWD

Command:

locate pwd

Output:


peida-VirtualBox ~ # locate pwd
/bin/pwd
/etc/.pwd.lock
/sbin/unix_chkpwd
/usr/bin/pwdx
/usr/include/pwd.h
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py
/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py
/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc
/usr/lib/syslinux/pwd.c32
/usr/share/help/C/empathy/irc-join-pwd.page
/usr/share/help/ca/empathy/irc-join-pwd.page
/usr/share/help/cs/empathy/irc-join-pwd.page
/usr/share/help/de/empathy/irc-join-pwd.page
/usr/share/help/el/empathy/irc-join-pwd.page

Example 2: search all files beginning with SH in etc directory

Command:

locate /etc/sh

Output:


peida-VirtualBox ~ # locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
peida-VirtualBox ~ #

Example 3: search for all files starting with m in etc directory

Command:

locate /etc/m

Output:


peida-VirtualBox ~ # locate /etc/m
/etc/magic
/etc/magic.mime
/etc/mailcap
/etc/mailcap.order
/etc/manpath.config
/etc/mate-settings-daemon

-E will be excluded from the search.

-E means if you have just deleted a file and the / var / lib / mlocate / mlocate.db database file has not been updated yet. After using the locate file name directly, the file that has just been deleted will still be listed. However, if you use the – e command, the file path will be listed only after confirming that the file is in the file system. Therefore, if there are many matching files, the – e option will slow down the query speed.

When running the locate command, the system reports an error: locate: command not found. After checking the relevant information, we found that the reason is that there is no executable file on the system, so we need to install it. The solution is as follows:

Execute command:[ [email protected] ~]#Yum install locate / / see which package the locate belongs to

The above is Xiaobian to introduce you to a daily Linux command of the locate command, I hope to help you, if you have any questions, please leave me a message, Xiaobian will reply to you in time. Thank you very much for your support to developer!