Linux – locate Command

The locate command in Linux is used to find the locations of files quickly. It searches through a database of file names and paths, which is usually updated periodically. This makes it much faster than commands like find for locating files.



Tutorials dojo strip

Initial Example

Using locate to find files that contain the word “example”:

locate example




LOCATE Parameters

ParameterDescription
-0, --nullUse ASCII NUL as the separator instead of newline.
-A, --allOnly print names matching all non-option arguments.
-b, --basenameMatch the pattern against the base name of files.
-c, --countPrint the total number of matches instead of file names, unless --print is also present.
-d path, --database=pathSearch the specified file name databases instead of the default.
-e, --existingOnly print names of files that currently exist.
-E, --non-existingOnly print names of files that do not currently exist.
--helpDisplay help information and exit.
-i, --ignore-caseIgnore case distinctions in patterns and file names.
-l N, --limit=NLimit the number of matches to N.
-L, --followTreat broken symbolic links as non-existing files (default behavior).
--max-database-age DChange the maximum age of the locate database before a warning is issued (default is 8 days).
-m, --mmapAccepted for compatibility with BSD locate, but does nothing.
-P, -H, --nofollowTreat broken symbolic links as existing files.
-p, --printPrint search results when normally they wouldn’t due to --statistics or --count.
-r, --regexInterpret the pattern as a regular expression.
--regextype RUse the specified regular expression dialect R.
-s, --stdioAccepted for compatibility with BSD locate, but does nothing.
-S, --statisticsPrint statistics about each locate database and exit without searching.
--versionDisplay version information and exit.
-w, --wholenameMatch against the entire file name as listed in the database (default behavior).




Examples

1. Basic Usage

To find all files and directories that contain the word “document”:

locate document

2. Case-Insensitive Search

To perform a case-insensitive search, use the -i option:

locate -i document

3. Limiting the Number of Results

To limit the number of results, use the -n option followed by the number of results you want:

locate -n 5 document

4. Updating the Database

To ensure the locate database is up-to-date, use the updatedb command:

sudo updatedb

5. Search by File Extension

To find all files with a specific extension (e.g., .txt):

locate '*.txt'

6. Search for Exact Matches

To find files that exactly match a specific name, use the -r option with a regular expression:

locate -r '^/etc/hosts$'

7. Excluding Specific Directories

To exclude specific directories from the search, use the -e option:

locate -e /bin /usr




Linux Playground

Scroll to Top