Linux locate
Command
The locate
command in Linux is used to search for files in a system. It relies on a pre-built database that contains the paths of files and directories. The locate
command is faster than the find
command as it searches the pre-built database rather than the file system.
Syntax
locate [OPTION]... PATTERN...
Options: Here are some useful options for the locate
command:
-A
or --all
: Display only entries that match all patterns instead of requiring only one of them to match.
Example:
locate -A file1 file2
-b
or --basename
: Match only the base name against the specified patterns.
Example:
locate -b filename
-c
or --count
: Write the number of matching entries instead of writing file names.
Example:
locate -c filename
-d
or --database DBPATH
: Replace the default database with DBPATH.
Example:
locate -d /path/to/custom/database filename
-h
or --help
: Display help documentation.
Example:
locate -h
-i
or --ignore-case
: Ignore case sensitivity of the specified patterns.
Example:
locate -i FILE
-l
or --limit
or -n LIMIT
: Exit successfully after finding LIMIT entries.
Example:
locate -l 5 filename
-m
or --mmap
: Ignore compatibility with BSD and GNU locate.
Example:
locate -m filename
-0
or --null
: Separate entries with the ASCII NUL character instead of writing each entry on a separate line.
Example:
locate -0 filename
-S
or --statistics
: Write statistics about each read database to standard output.
Example:
locate -S
-r
or --regexp REGEXP
: Search using a basic regexp REGEXP.
Example:
locate -r 'pattern.*'
--regex
: Describe all patterns as extended regular expressions.
Example:
locate --regex 'pattern.*'
-V
or --version
: Display version and license information.
Example:
locate -V
-w
or --wholename
: Match only the whole path name in specified patterns.
Example:
locate -w filename
Examples:
Basic Usage:
locate filename
Limiting Search Results:
locate -n 10 "*.txt"
Displaying the Number of Matching Entries:
locate -c filename
Ignoring Case Sensitivity:
locate -i "siliconvlsi.txt"
Updating the mlocate
Database:
sudo updatedb
Displaying Only Available Files:
locate -i -e *siliconvlsi.txt*
The locate
command is a powerful tool for quickly searching for files on a Linux system based on a pre-built database, providing faster results compared to some other file search methods.