Linux locate command
The Linux locate command is used to find files that meet certain criteria; it searches a database that stores file and directory names for files or directories matching a pattern.
In general, we only need to enter locate your_file_name to find a specified file.
Syntax
locate [-d ][--help][--version][范本样式...]
Parameters:
- -b, --basename -- match only the base name of the path
- -c, --count -- output only the number of matches 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 files that currently exist
- -1 -- if the value is 1, enable safe mode. In safe mode, users will not see files they do not have permission to see. This slows things down, because locate must obtain file permission information from the actual file system.
- -0, --null -- separate entries with NUL in the output
- -S, --statistics -- do not search entries; print statistics about each database
- -q -- quiet mode; do 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
Example
To find the passwd file, enter the following command:
locate passwd
Search for all files starting with sh in the etc directory:
locate /etc/sh
Case-insensitively search for all files starting with r in the current user's home directory:
locate -i ~/r
Additional Notes
locate differs from find: find searches the hard disk, while locate only searches the /var/lib/slocate database.
locate is faster than find; it does not really search but queries a database. The file database is usually at /var/lib/slocate/slocate.db, so locate lookups are not real-time but based on database updates. It is generally maintained by the system, but you can also update the database manually with the following command:
updatedb
By default, updatedb runs once a day.
Linux Command Reference