A Linux command a day (16): locate command

post thumb
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.


locate pwd


peida-VirtualBox ~ # locate pwd  

Example 2: Search for all files starting with sh in the etc directory


locate /etc/sh


peida-VirtualBox ~ # locate /etc/sh  

peida-VirtualBox ~ #

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


locate /etc/m


peida-VirtualBox ~ # locate /etc/m  



comments powered by Disqus