locate Command: Tutorial & Examples
Search for files and directories using a database
The locate
command is a utility in Linux that allows you to search for files and directories on your system by name. It uses a database of file names that is updated periodically
to quickly locate files. Because the locate command uses a database rather than searching the file system directly, it is generally much faster than other search utilities, such
as find
.
Here is the basic syntax for using locate:
locate [options] pattern
The pattern
argument is a string that specifies the name of the file or directory that you want to search for. You can use wildcard characters, such as *
and ?
, to match
multiple files.
Here are some common options for locate
:
-b
: Searches for the pattern as a whole word.-c
: Displays the number of matches found, but not the actual file names.-d path
: Specifies a different database to use for the search.
Here is an example of using locate
to search for a file named example.txt
:
locate example.txt
This command will search the locate
database for any files or directories named example.txt
and display their full paths.
It's worth noting that the locate
command may not be able to find recently created or modified files because the locate database is typically updated periodically. To search for
recently created or modified files, you can use the find
command instead.