locate Command in Linux
The locate
command in Linux is a file-searching command-line tool used to quickly find files and directories based on their names by searching updated databases.
To use the locate
command run the following syntax in the terminal and find the location of the files in your Linux system simply and quickly:
locate [filename or part of the filename]
Steps to Install locate in Linux
Dealing with files and directories is one of the daily tasks of Linux administrators and gaining skills in managing files can improve your productivity especially when using Linux VPS.
There are several methods to find the files and directories in Linux, but the locate
command is the most convenient and quick way. The locate
command is an efficient alternative to the find command for file searching because it searches a pre-built database (that is regularly updated with information about all files) instead of recursive searching the entire filesystem.
If the locate
is not available in your Linux system, here are the steps to install locate
in Linux distributions such as Debian, Ubuntu, CentOS, RHEL, and Fedora. Therefore login to your Linux VPS as root and follow the below steps with sudo privilege:
1. Check whether locate is installed:
First, check whether the locate
command is installed in your Linux system or not by running the following command:
locate
Output:
Command ' locate' not found
This output indicates your Linux system does not come with the locate
utility preinstalled and you must install locate
on your Linux system.
However, if you get ” locate: no pattern to search for specified” output, it means the locate
utility is installed on your Linux system and you can skip the locate
installation section.
2. Update the package repository:
Update your package repository using specific package managers of your Linux distro:
sudo apt update #Debian/Ubuntu
sudo yum -y update #CentOS/RHEL/Fedora
3. Install locate on Linux distros
Using your distro package manager, install locate
on install:
sudo apt install mlocate # Debian/Ubuntu
sudo yum install mlocate # CentOS/RHEL 7 or earlier
sudo dnf install mlocate # CentOS/RHEL 8+, Fedora
Now, your Linux system is equipped with a locate
utility. Installing mlocate package contributes to updating the database every 24 hours by default via the created cron job, however, to manually update the database, run the following command:
sudo updatedb
This command ensures that the locate
utility obtains the result from an up-to-date database of your files.
Sometimes, the locate
can not find some files that have been recently created, therefor manually updating the database can be the solution to find recently created or moved files.
How to use the locate command in Linux?
To use the locate
command in Linux you must run the locate
command with the name of the files or directories to gain the paths of all files that match the specified name or pattern.
locate basic syntax in Linux:
The basic syntax of the locate
command in Linux is as follows:
locate [options] [pattern]
The locate
command without options prints the path of files and directories that match the search pattern for those who have read permission. For example, if you run the locate
command as a normal user, you can not access /root directory because you do not have root permission.
locate command key options in Linux:
The locate command in Linux like other command line utilities has numerous options to tailor locate searches to your specific needs. Here are some key options:
- -A: Prints the entries that match all the given patterns.
- -b: Exclusively matches the base name with the specified patterns.
- -c: Shows total number of matches found instead of filenames.
- -d: Uses a different database file (DBPATH, a colon-separated list of database file names) instead of the default one (default is /var/lib/locate.db).
- -e: Searches for entries that refer to existing files.
- -r (–regexp): Uses a regular expression instead pattern for a more advanced search.
- -q: Quiet mode, suppress output to not show any messages containing errors while reading or processing databases.
- -i: Ignores pattern case sensitivity when searching file name.
- -n: Limits the number of results displayed.
- -S: Displays statistics about each read database instead of finding files.
- -h: Prints a help message.
- -V: Shows the version of the locate command.
- -w: Only matches the whole path name to the given patterns.
Basic search for a file
The locate
command can print the path of files and directories that you are searching for using the file name or part of the file name. For example, to search for a file named “test.txt” you would type:
locate test.txt
This command will display all file paths that contain “test.txt”.
Customizing the locate output
You can format the output of the locate
command in Linux when the result is too long by piping the output to the less
command in Linux:
locate test.txt | less
Piping the output to less
command results in better readability.
Limiting the Search Results
You can limit the number of the search result using the -n
option. For example to display only 15 results for searching of file ending with .html run the following command:
locate "*.html" -n 15
This command returns first 15 files matching the search pattern.
Note: The locate
command accepts patterns containing globbing characters (such as *, ?, etc) for flexible pattern matching. If the pattern does not include globbing characters, the locate
command searches for *PATTERN*. Therefore using a wildcard in the previous command enhances the power and precision of the search by finding files based on matching with specific name patterns (such as files with partial names or certain extensions).
Case-Insensitive Search
The locate
command by default processes queries in case-sensitive manner which using -i
option enforces locate
command to ignore case when searching. For example to ignore case sensitive use the following command:
locate -i test.txt
Output:
/home/opera/p1/TEST.txt
/home/opera/p2/Test.txt
/home/opera/p3/test.txt
Displaying existing files only
As we mentioned earlier, since the locate
utility searches from the database, you must always be sure your database is up to date. Because, if your database has not been updated, the locate
command may print deleted files or may not show recently created files that were created after the last database update.
Therefore, to avoid encountering this problem, you must manually update the database before running the locate
command or use the -e
option to show only existing files when running the locate command in Linux:
locate -e filename
For example, we delete the desired file named test.txt, and run the locate command:
rm test.txt
locate test.txt
Output:
/home/opera/p3/ test.txt
This output represents that the locate
command returns the path of the test.txt while we have deleted it. Therefore, use the -e
option with locate to eliminate these shortcomings:
locate -e test.txt
This command returns no result because the file was deleted.
Counting number of matching entries
Sometimes you need to know how many specific files which are important to you, are existed on your system. In this situation -c
option with locate command can help you see the number of matched files instead of filenames and their paths:
locate -c .txt
Output:
183
As you see this command returns the number of all matching entries, all files ending .txt.
Suppressing output of the locate in Linux
You can enable a quiet mood while searching using -q
option with locate
command in Linux to suppress any error messages or warning that might appears while processing databases. To do this use the following example:
locate -q myfile.txt
This command will find myfile.txt but will not display any error if they occur. This is useful when you want to suppress permission errors.
Searching file with the exact name
By default, the locate
command searches based on the pattern that indicates the filename or part of the filename.
Also, the locate
command supports complex search, allowing you to use basic regexp (regular expression) instead of the pattern. Therefore, to find a file with an exact name (files ending with .txt) run the following command:
locate -r \.txt$
This command shows the files whose filename exactly matches the input query. The forward slash character (/) is used for regular expression boundaries and typing the dollar sign ($) at the end specifies the end of a line.
Using different Database when searching
You can specify a different database instead of using the default database when you need to find the results that do not exist in the default mlocate database. To use a different database, run the following syntax:
locate -d [new database path] [pattern]
Checking Database Statistics
The locate
command allows you to check the statistics of the database and gain information such as the number of files indexed, the number of directories and files, and the size of the database. To display statistics about the locate database instead of searching for files, use the -S
option with the locate command:
locate -S
Output:
Database /var/ lib/mlocate/mlocate.db:
58,150 directories
276,768 files
22,067,113 bytes in file names
18,420,189 bytes used to store database
Using different separators for locate command’s output
The newline (\\n) character is the default separator for the locate command output entries, however, you can use a different separator like the ASCII NULL using the -0
option:
locate -0 test.txt
Can locate find the hidden files?
Yes, the locate
command can find hidden files which typically begin with the dot (.) if they are indexed in the database. For example, to find any files named .bashrc including hidden ones, you can run the following command:
locate .bashrc
If the hidden files were created after the last database update, you might need to manually update the locate database to ensure the hidden files are indexed.
How can I find a directory in Linux using locate command?
To search a specific directory using locate
command, use the following syntax:
locate -r '/directory_name$'
Or
locate directory_name
Using trailing slash (/) is useful for limiting the results to directories and using the -r
option is for ensuring you are finding directories and searching the path ending with the directory name.
However, this command will list all paths containing directory_name which sometimes will return too many results. you can use the find
command as a better alternative to the locate
command for finding directories with the below syntax:
find /path/to/search -type d
Or
find / -type d -name directory_name
Conclusion
The locate
command in Linux is a powerful command line tool for quick and efficient search for files. Becoming an expert in the locate
command has a valuable advantage in managing files in a large file system. To gain more information about the locate
command, run the ” man locate
” in the terminal.