Updated April 14, 2023
Introduction to Linux Locate Command
In the Linux ecosystem, the locate command is used to find or search the files by their names. It is more powerful and provides quick results as compared to find the command.
Note: As per the other file or directory searching utilities, locate command will not search the complete file system to find the file or file location. It will use the DB for finding the file location. The Locate command utility was written by Miloslav Trmac.
Syntax:
locate [OPTION]... PATTERN...
- locate: We can use the locate keyword in the syntax or command. It will take the two-argument as an option and file name or pattern. Provide the end result to search the file location on the file system.
- OPTION: We can provide the different flags as options that are compatible with the locate command
- FORMAT: As per the requirement, we can pass the different types of patterns or file names to the Locate command.
How does Linux Locate Command Work?
As discussed earlier, the locate command is used to search or identify the files by their name. Basically, the locate command is very light and faster. Because there is a background process in the system, it will run in the Linux system. The process is running continuously and finds a new file and stores relative information in the database. When any client will use the locate command, the client request will only search the filename in the database only. It will not search for the complete file system. Hence the locate command is better and faster as compared to other searching utility.
The locate command will be able to read one or more databases prepared by updatedb – 8 and writes file names those are matching at least one of the patterns (as per the standard output). Patterns can contain globbing characters if the –regex values are not provided.
By default in locating a command, it does not check whether the files are present in the database but it does require all parent directories which are available in the Linux Environment should be present in the database. Once the most recent file will generate or created in the environment. The locate utility will never report updating the new file information in the relevant database.
Examples to Implement Linux Locate Command
Following are the examples of linux locate command is given below:
1. LocateCommand
It is a very simple and common way to use the locate command in a Linux environment. It will print the file location, where is the file present in the Linux operating system.
Command:
locate test.txt
Explanation: With the help of locating a command, we are able to find the file location on the system. As per screenshot 1(a), we have created a sample file and locate the file location with the help of locating command.
Output:
cd /data/
ls
touch test.txt
cd
pwd
locate test.txt
2. Locate Command – Limit Search Query
We are able to limit the search query in the locate command. To get a limited list of output, we need to use the “-n” option with locate command.
Command:
locate passwd -n 5
Explanation: By default, locate command will print all the pattern matching file location. As per the above command, we are able to print the initial matching 5 location.
Output:
locate passwd
locate passwd -n 5
3. Locate Command – Print the Number of Matching Entry
In Locate command, we are having the functionality to get the count of matching files or file location on the environment. To get the number of matching count, we need to use the “-c” option with locate command.
Command:
locate -c [passwd]*
Explanation:
As per the above locate command, we are able to print the location path (passwd) on integer value i.e. there is 64 location where passwd pattern will present.
Output:
Locate -c passwd] *
4. Locate Command – Ignore Case Sensitive
In Locate command, we are able to print all the upper case and lower case file location. To print all the insensitive file location, we need to use the “-i” option with locate command.
Command:
locate -i *test*.txt*
Explanation:
By default, the locate command is case sensitive. As per the above command, we are searching for the file location without case sensitive.
Output:
touch /data/TEST.TXT
touch /data/TEST1.TXT
locate -i *test.txt*
locate -i *test.txt*
5. LocateCommand–Display only Present File
To avoid the deleted file information and need to get the present file only. We can use the “-e” option with the locate command.
Command:
locate -i -e *test.txt*
Explanation: When any physical file will remove from the file system still locate command will display the location. To avoid this condition and get the correct data, we need to use the “-e” option with the command.
Output:
rm /data/TEST*
locate -i -e *test.txt*
6. Locate command – Print Output without New Line
In the Linux environment, we can use the locate command in a different way. Here, we are able to print the locate command output in sequential manager i.e. not in the new line.
Command:
locate -i -0 *passwd*
Explanation: As per the requirement, we can print the locate command output in the single line without starting with the new line.
Output:
locate –i -0 *passwd*
7. Locate Command – Review Locate Database
In locate command, we are having the functionality to review the locate database. We can use the “-S” option with the locate command.
Command:
locate -S
Explanation: As per the above command, we are able to do the overview of the locate database.
Output:
locate -S
8. Locate Command – Database Update
In Locate command, the new files will not reflect at the same time of file creation. We can use the below command to update the locate database and able to find the file location.
Command:
updatedb
Explanation: To get the correct and updated file information, we need to run the above command. It will update all the new file creation in the Linux ecosystem.
Output:
updatedb
Conclusion
We have seen the uncut concept of “Linux Locate Command” with the proper example, explanation, and command with different outputs. The locate command is working on its own database. Hence it is more fast and quick to find the file location on the Linux environment.
Recommended Articles
We hope that this EDUCBA information on “Linux Locate Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.