A Linux command a day (16): locate command

post thumb
Devops
by Admin/ on 28 Sep 2021

A Linux command a day (16): locate command


locate allows users to quickly search for a specific file in the file system. This is done by first creating a database of all the files in the system with their names and paths, and then simply querying this database when looking for them, without actually going into the file system. In general distribution, the creation of the database is automatically executed in crontab.

1. Command format.

locate [select parameters] [style]

2. Command function.

The locate command can quickly find files when searching the database, which is updated by the updatedb program, and updatedb is created periodically by the cron daemon. If the files found by locate are recently created or just renamed, they may not be found, and in the default values, `updatedb' will run once a day, and the settings can be updated by modifying the crontab. (etc/crontab)

locate is used to search for files that meet the criteria, it will go to the database where the names of files and directories are stored, and look for files or directories that meet the criteria of the model style. For example, if you specify kcpa_ner, locate will find all files or directories that start with the string kcpa and end with ner, for example, if the name is kcpartner, if the directory name is kcpa_ner, it will list all files under that directory, including subdirectories.

The locate command is similar to the find command, but the locate command creates an index database of all files and directories on the hard disk through the update process, and then looks for the index directly when executing loacte, which is faster.

3. Command parameters.

-e will exclude from the search.

-1 If it is 1, safe mode is activated. In safe mode, users will not see files that are not visible to their permissions. This slows things down because locate must go to the actual file system to get the file's permissions information.

-f excludes specific filesystems, e.g. we have no reason to put files from the proc filesystem in the repository.

-q Quiet mode, no error messages will be displayed.

-n Display up to n outputs.

-r Use regular arithmetic as search condition.

-o Specifies the name of the data inventory.

-d Specify the path to the database.

-h Display auxiliary messages

-V Display program version information

4. Examples of use.

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 for all files starting with sh in the etc directory

Command:

locate /etc/sh

Output:

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

peida-VirtualBox ~ #

Example 3: Search the etc directory for all files starting with m

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  

Reference:

www.cnblogs.com/peida/archive/2012/11/12/2765750.html

comments powered by Disqus