Linux locate Command
The Linux locate command is used to find documents that match specified conditions. It searches within a database that stores document and directory names to locate files or directories that match the pattern.
Typically, you only need to enter locate yourfilename to search for a specific file.
Syntax
locate [-d ][--help][--version][pattern...]
Parameters:
-b, --basename -- Match only the base name of the path
-c, --count -- Output only the count of found entries
-d, --database DBPATH -- Use the database specified by DBPATH instead of the default /var/lib/mlocate/mlocate.db
-e, --existing -- Print entries only for currently existing files
-1 -- If set to 1, enable safe mode. In safe mode, users won't see files they don't have permissions to view. This slows down the process as locate needs to retrieve file permission data from the actual file system.
-0, --null -- Output entries separated by NUL characters
-S, --statistics -- Do not search entries, print statistics about each database
-q -- Quiet mode, does not display any error messages
-P, --nofollow, -H -- Do not follow trailing symbolic links when checking file existence
-l, --limit, -n LIMIT -- Limit the 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, does not display any error messages
-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
Examples
To find the passwd file, enter the following command:
locate passwd
To search for all files in the etc directory that start with sh:
locate /etc/sh
To ignore case and search for all files in the current user's directory that start with r:
locate -i ~/r
Additional Notes
Unlike find, locate does not search the hard drive; it only searches within the /var/lib/slocate database.
locate is faster than find because it does not perform real-time searches but relies on a database. The file database is typically located at /var/lib/slocate/slocate.db, and locate searches are based on this database's updates, which are usually maintained by the system. You can also manually update the database with the command:
updatedb
By default, updatedb runs once a day.