Common linux commands: locate command


  locate allows users to quickly search for a specified file in the file system. The method is to first establish a database including all file names and paths in the system, and then only need to query this database when searching, without actually going deep into the file system. In the general distribution, the establishment of the 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 is periodically established by the cron daemon. The locate command searches the database faster than the entire hard disk data. If the file found by locate is recently created or renamed, it may not be found. In the default value, updatedb will run once a day, and the setting value can be updated by modifying crontab. (etc/crontab)

  locate specifies that it is used to search for files that meet the conditions. It will go to the database storing file and directory names to find files or directories that meet the template style conditions. Special characters (such as "*" or "?", etc.) can be used to Specify the template style. If the specified template is kcpa*ner, locate will find all files or directories whose starting string is kcpa and ending with ner. All files including subdirectories.

  The locate command is similar to the function of find to find files, but locate is to create an index database for all files and directory data in the hard disk through the update program, and directly find the index when executing locate, the query speed will be faster, and the index database is generally It is managed by the operating system, but it is also possible to directly issue an update to force the system to modify the index database immediately.

3. Command parameters:

-e will exclude from seeking.

-1 if is 1. Start Safe Mode. In safe mode, users will not see files that permissions cannot see. This will start to slow down because locate has to go to the actual filesystem to get permission data for the file.

-f excludes certain filesystems, for example we don't have any reason to put files in the proc filesystem in the database.

-q Quiet mode, no error messages are displayed.

-n Display at most n outputs.

-r Use regular expressions as search conditions.

-o Specifies the name of the repository.

-d specifies the path to the database

-h show auxiliary information

-V display program version information

4. Example of use:

Example 1: Find all files related to pwd

Order:

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 

Order:

locate /etc/sh

output:

peida-VirtualBox ~ # locate /etc/sh

/etc/shadow

/etc/shadow-

/etc/shells

hide -VirtualBox ~ #

 

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

Order:

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325210057&siteId=291194637