loading...
Cover image for Locating Files and Directories in Linux System

Locating Files and Directories in Linux System

yashsugandh profile image yash sugandh ・4 min read

linux for begineers (11 Part Series)

1) Linux Navigation For Beginners 2) What are Commands in Linux? 3 ... 9 3) Exploring Linux System 4) Wildcards in Linux System 5) Creating Files and Directories in Linux System 6) Deleting Files and Directories in Linux System 7) How to Copy, Move and Rename Files and Directories in Linux System 8) Locating Files and Directories in Linux System 9) Finding Files and Directories in Linux System 10) I/O Redirection In Linux System 11) Piping in Linux System

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 locateand 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

locate file syntax

To use the locate command we only need to pass the file_name we want to find.

locate-single-file

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.

locate-update-db

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

locate-insensitive

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

locate-limit-text

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

locate-count-total

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

locate-existing-count

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.

linux for begineers (11 Part Series)

1) Linux Navigation For Beginners 2) What are Commands in Linux? 3 ... 9 3) Exploring Linux System 4) Wildcards in Linux System 5) Creating Files and Directories in Linux System 6) Deleting Files and Directories in Linux System 7) How to Copy, Move and Rename Files and Directories in Linux System 8) Locating Files and Directories in Linux System 9) Finding Files and Directories in Linux System 10) I/O Redirection In Linux System 11) Piping in Linux System

Posted on May 26 by:

yashsugandh profile

yash sugandh

@yashsugandh

A software engineer trying to figure out how stuff works

Discussion

markdown guide