Till now we have studied commands for navigation, exploration, creation, deletion, and manipulation of files and directories.
Doesn't it feel like we are still missing something?
Yeah you are right. We are still missing one of the most important functionality to search for files in our Linux System.
Two of the most popular and trusted file-searching command-line utilities are locate
and find
.
1. locate
command
The locate command is used to search and locate the file. It is blazing fast in locating the files as compared to its counterpart.
The syntax for locate
command
To use the locate
command we only need to pass the file_name we want to find.
In the above example, we used the locate
command to search for a file "file_to_be_located" and the locate command returned us the location of the file.
But how does the locate command work? Why is it so fast?
The reason locate
is so fast is because it doesn't read the file system for the searched file or directory name.
It actually refers to a database updatedb which is created by the user nobody and is automatically updated daily by a cronjob.
While this is a good approach, it has its share of drawbacks.
Since the database is only updated by a cron job the new files that we have created after the database was updated will not be visible to locate command
.
To fix this we can manually update the db using the command sudo updatedb
.
Let's look at an example of how to manually update the updatedb.
In the above example we used the command locate -S
to get the statistics of database that locate
command uses for searching.
Then we used the command sudo updatedb
to update the database.
After the completion of the sudo updatedb
command we again used the command locate -S
and found out that the number of files have been increased after the database was updated.
Let's look at some more examples for better understanding
- locate all the files named "textFile" irrespective of its case
In the above example we first used the command locate textFile
to get all the files with name "textFile".But since we need all the results we need result case-insensitive
For that we used locate -i textFile
where
locate
represents the locate command
-i
represents case-insensitive
.txt
represents all the text files
- locate all the text files but limit the output to 10
In the above example, we used the command locate .txt -n 10
where
locate
represents the locate command
.txt
represents all the text files
-n 10
represents the number of entries we want i.e. 10 in our case
We can also use long format option --limit
instead of -n
for limiting the number of entries.
- count number of text files in Linux System
In the above example, we use the command locate -c .txt
where
locate
represents the locate command
-c
represents count of entries
.txt
represents all the text files
Wait a minute there could be another problem because of the updatedb updating only by a cronjob.
What will happen if we delete some files and database is not yet updated?
Hmmm, In that case we will get even those files that do not exist on the present system.
Can we do something so that all these files can be skipped?
Yes ofcourse let's check it out
- count number of text files currently existing in Linux System
In the above example, we used the command locate -ec .txt
where
locate
represents the locate command
-e
represents existing entries of file specified
c
represents count of entries
.txt
represents all the text files
From the above exampe we can see that the previous count of text files was 2932 and the current count is 2925.
So 7 text files were deleted after the database was last updated.
Okay, so that’s all need to know about locate
command.
In the next post we will have a look at what is find
command, how does it work, how is it different from locate
command and so on.
I hope you understood locate
command-line utility in Linux. Please, let me know if there are any questions.
Top comments (0)