locate – Linux, Unix Command

About

locate command finds files by name. This command is fast because it has contents that is stored in the database. On regular basis locate runs a cron that updates the system. So instead of having search the entire system when you use locate. It is searching the database. Remember, the database must always be up-to-date if you want to find recently created files.

The database can be updated manually by typing the following command:

updatedb

Syntax

locate [OPTION]… PATTERN…

Examples

1.) This command locates files named hello on the system.

locate hello

2.) This command locates files named pwd on the system.

locate pwd
/etc/.pwd.lock
/usr/bin/pwd
/usr/bin/pwdx
/usr/lib/modules/3.10.0-327.el7.x86_64/kernel/drivers/watchdog/hpwdt.ko
/usr/lib/x86_64-redhat-linux6E/include/pwd.h
/usr/lib64/cracklib_dict.pwd
/usr/lib64/python2.7/site-packages/ldap/controls/pwdpolicy.py
/usr/lib64/python2.7/site-packages/ldap/controls/pwdpolicy.pyc
/usr/lib64/python2.7/site-packages/ldap/controls/pwdpolicy.pyo
/usr/sbin/unix_chkpwd
/usr/share/cracklib/cracklib-small.pwd
/usr/share/cracklib/pw_dict.pwd
/usr/share/doc/krb5-workstation-1.13.2/user/pwd_mgmt.html
/usr/share/man/man0p/pwd.h.0p.gz
/usr/share/man/man1/pwd.1.gz
/usr/share/man/man1/pwdx.1.gz
/usr/share/man/man1p/pwd.1p.gz
/usr/share/man/man3/lckpwdf.3.gz
/usr/share/man/man3/ulckpwdf.3.gz
/usr/share/man/man8/unix_chkpwd.8.gz
/usr/share/man/overrides/de/man1/pwdx.1
/usr/share/man/overrides/fr/man1/pwdx.1
/usr/share/man/overrides/uk/man1/pwdx.1

3.)  -c option to locate, display the count of the number of matching entries.

locate -c pwd
24

Locate OPTIONS with Description as specified in man pages

-b, –basename
This option matches only the base name against the specified pattern on the terminal. It is the opposite of –wholename.

-c, –count
-c option writes the number of matching entries instead of writing file names on standard output.

-d, –database
-d replaces the default database with DBPATH.

An empty database file name is replaced by the default database available. A database file name – refers to the standard input.

-e, –existing
It displays only that entries which refer to files existing at a time when locate is run.

 

-h, –help
It displays a summary of the all available options to terminal output and then exits successfully.

-i, –ignore-case
-i ignore case distinctions when matching patterns.

-l, –limit,
It exits successfully once LIMIT entries are found.

-S, –statistics
Instead of searching for files. It writes statistics about each read database to standard output and then exits successfully.

–regex
Interpret all PATTERNs as extended regexps.

-V, –version
-V option write information about the license and version of locate on the terminal and then exits successfully.

-w, –wholename
-w matches only the whole path name with the specified pattern.

This behavior is the default. The opposite of it can be specified using –basename.

Related commands

find — Find files within a directory hierarchy.
which – shows the full path of (shell) commands.
whereis — locate the source, binary, and manual page files (man) for a command.

Add Comment