locate

A file search tool that is easier to use than find

Supplementary instructions

locate allows users to quickly search whether there are specified files in the file system. The method is to first create a database that includes the names and paths of all files in the system. Then when searching, you only need to query this database without actually going deep into the file system. In a general distribution, database creation is automatically executed in crontab.

The locate command can quickly find files when searching the database. The database is updated by the updatedb program. updatedb is periodically created by the cron daemon. The locate command is faster when searching the database than searching for data from the entire hard disk, but it is inferior. If the file found by locate was recently created or renamed, it may not be found. In the default setting, updatedb will run once a day. You can modify the crontab to update the setting value. (etc/crontab)

locate is designated to search for files that meet the conditions. It will search for files or directories that meet the template style conditions in the database that stores file and directory names. You can use special characters (such as "" or "?", etc.) Specify the template style. For example, if the specified template is kcpaner, locate will find all files or directories whose starting string is kcpa and ends with ner. For example, if the name is kcpartner and the directory name is kcpa_ner, the files or directories in the directory will be listed. All files including subdirectories.

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

grammar

locate [-d path | --database=path] [-e | -E | --[non-]existing] [-i |
        --ignore-case] [-0 | --null] [-c | --count] [-w | --wholename] [-b |
        --basename] [-l N | --limit=N] [-S | --statistics] [-r | --regex ]
        [--regextype R] [--max-database-age D] [-P | -H | --nofollow] [-L |
        --follow] [--version] [-A | --all] [-p | --print] [--help] pattern...

Options

-b, --basename # Match only the basename of the pathname
-c, --count # Only output the number found
-d, --database DBPATH # Use the database specified by DBPATH instead of the default database /var/lib/mlocate/mlocate.db
-e, --existing # Print only entries for currently existing files
-1 # if is 1. Then start safe mode. In safe mode, users will not see files with permissions that do not allow them to see. This will start to slow down because locate must go to the actual file system to obtain the file's permission information.
-0, --null # separate entries with NUL on output
-S, --statistics # Do not search for entries, print statistics about each database
-q #Quiet mode, no error messages will be displayed.
-P, --nofollow, -H # Do not follow trailing symbolic links when checking for file existence
-l, --limit, -n LIMIT # Limit output (or count) to LIMIT entries
-n # Display at most n outputs.
-m, --mmap # Ignored for backward compatibility
-r, --regexp REGEXP # Use basic regular expressions
     --regex # Use extended regular expressions
-q, --quiet #Quiet mode, no error messages will be displayed
-s, --stdio # Ignored for backward compatibility
-o # Specify the name of the database.
-h, --help # Display help
-i, --ignore-case # Ignore case
-V, --version # Display version information

Example

Example 1: Find all files related to pwd

root ~ # 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

root ~ # locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells

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

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

Ignore case and search all files starting with r in the current user directory:

locate -i ~/r