Here is how to use locate Command in Linux Ubuntu to search and find file in Linux terminal. If you get an error message – locate command not found – you will have to install the locate command in Linux Ubuntu manually.
The locate command in Linux is a powerful utility for quickly finding files and directories by name. Unlike the find command, which searches in real-time, locate uses a prebuilt database to provide fast search results.
As per the man page, for each PATTERN given ‘locate’ searches one or more file name databases returning each match of PATTERN.
NOTE: If the locate command package is installed, the system will display locate: no pattern to search for specified. Otherwise, you will see something like locate command not found.
Install locate on Ubuntu and Debian using the following commands:
sudo apt update
sudo apt install mlocate
Using Locate Command
Once installed, you can use the locate command. Using locate command is dead simple, you just have to specify the file name and the result will show all the places where the file you specified was found.
The syntax for the locate command is as follows:
locate [OPTION] PATTERN…
When you use the locate command without any options, the locate command will print the absolute path of all files and directories that matches the search pattern given by you and for which the user has read permission as per the file-system configuration.
NOTE: The locate command searches for a given pattern through a database file that is generated by the updatedb command. Once the pattern is matched and found, the found results are displayed on the screen, one per line.
The database can be manually updated by running updatedb command:
sudo updatedb
Once the command is executed the update process will take some time, depending on the number of files and directories on your system.
For example to search for a file named .sourcedigit you would run the following command:
locate .sourcedigit
The output will include the names all files containing the string .sourcedigit in their names:
If the result list is long, for better readability, you can pipe the output to the less command:
locate .sourcedigit | less
To limit the search results, use the -n option followed by the number of results you want to be displayed. Here the n tells to limit the number of results printed to N. When the locate command is used with the ‘–count’ option, the value printed will never be larger than this given limit.
For example, the following command will search for all sourcedigit files and display only 15 results:
locate -n 15 sourcedigit
By default, locate performs case-sensitive searches during its execution. If you want to to ignore the case and run a case-insensitive search you can use the command option -i (–ignore-case) with the locate command.
locate -i sourcedigit
NOTE: By default, locate doesn’t check whether the found files still exist on the file system or has been deleted. Even if you haev deleted a file after the latest database update (see above) the search will be included in the result.