The locate command is used to quickly search for files or directories within the indexes in the database file known as updatedb.
It is identical to the find command, except it will search in the database file, which is much faster than find’s method of searching rigorously throughout the entire system.
The updatedb database is set to automatically update in 24 hours using cron jobs when you install this program, but if you make any changes, it will not be able to locate them until cron jobs or you manually request the database update.
|Root or Sudo Privileges||Yes|
|Host System and Architecture||Ubuntu 22.10 (x64)|
|OS Compatibility||Ubuntu, Manjaro, Fedora, etc.|
|Internet Required||Yes (for installation)|
How to Install locate in Linux
If the command is missing from your distribution, execute one of the following commands to install it.
$ sudo apt install mlocate -y #For Ubuntu, Pop!_OS, etc $ sudo yum install mlocate -y #For Fedora, AlmaLinux, etc
Syntax of the locate Command
The locate command takes two arguments, one being the option and the other being the pattern.
$ locate [OPTION] [PATTERN]
The following is the list of valid options:
|It will use ASCII NUL as a separator instead of newline|
|It will only print the names that match the entire pattern|
|It will look for the final component (also known as the basename) for a match|
|It will print the total number of matches instead of their names|
|Instead of using the default database path to search, assign your custom database path to search for names|
|Only print out names that currently exist (instead of names that existed when the database was created).|
|Only print out names that currently do not exist (instead of names that existed when the database was created).|
|Ignore case sensitivity for both the pattern and the file names|
|Limit the number of searches assigned to this flag|
|If you are searching for files with “the |
|A database that has not been updated for more than 8 days will show you a warning message, but you can replace the X days with a custom value using this flag|
|The opposite of |
|Used for searching a basic regular expression |
|Print various statistics about each database to standard output instead of searching for files|
|Match the whole name of the file as listed in the database|
|-h, –help||Display the help section|
Manually Updating the Database
The updatedb database is set to automatically update within 24 hours using cron jobs, but you can manually update the database after every file change using the following command.
$ sudo updatedb
Searching for a Filename
The easiest way to search for any file is to assign its name as an argument to the locate command to search in the database.
$ locate nginx
The above command will print the absolute path with the Nginx pattern in the filename.
Searching for the Exact Filename
The following command will print the absolute path ending with the Nginx pattern.
$ locate */nginx
Limiting the Number of Searches
By default, the locate command will print the absolute path for all the matched filenames, but you can limit the search result to a specific number using the “
-l” or “
$ locate -l 5 nginx
Display the Number of File Occurrences
Use the “
-c” or “
--count” flag to print the total number of matches instead of their name.
$ locate -c nginx
The following command will print the total number of matches for the exact matched filename.
$ locate -c */nginx
The following command will print the total number of matches for the specific file type (ex: “
txt” or “
$ locate -c "*.txt" $ locate -c "*.jpg"
Ignore Case Sensitivity
By default, the locate command will search for the file using case-sensitivity, meaning “
file.txt” and “
File.txt” are not the same.
However, you can ignore the case-sensitivity using the “
-i” or “
--ignore-case” flag, which will include “
file.txt” and “
File.txt” while searching.
For example, I’ve created multiple “
.txt” files with different case-sensitivity in my home directory and issued “
sudo updatedb” to update database, as shown.
$ ls -l *.txt
If you search for these files using “
file.txt” as an argument without any flags, it will only output one result with the exact matching pattern.
$ locate ~/*file.txt*
But if you use the “
-i” or “
--ignore-case” flag, it will print all the files without considering the case-sensitivity.
$ locate -i ~/*FILE.txt*
Searching Only for Existing Files
If you have deleted a file between the automatic database update set in cron jobs (ex: “24 hours”), your files might still appear in the output because the database still has the file record.
For example, I’ve updated the updatedb database after creating the “
file.txt” file as shown.
$ ls file.txt $ locate ~/file.txt
For instance, if the file is deleted without refreshing the updatedb database, it will still appear in the output when searched.
$ rm file.txt $ locate ~/file.txt
To ignore the deleted files that come up in the search, use the “
-e” or “
$ locate -e ~/file.txt
Choose a Different Updatedb Location
By default, the locate command will use the default database located at the “
However, you can specify a custom database path using the “
LOCATE_PATH” environmental variable or by using the “
-d” or “
--database” flag with the database path.
$ locate -d ~/database.db ~/file.txt
And here comes the end of this article.
If you have any questions or queries, feel free to ask them in the comment section.