Locating Files and Directories in Linux System
Tags: linux, ubuntu, beginners, computerscience
Published: 2020-06-02 13:10:09

Till now we have studied commands for **[navigation](https://dev.to/yashsugandh/linux-navigation-for-beginners-406n)**, **[exploration](https://dev.to/yashsugandh/exploring-linux-system-279g)**, **[creation](https://dev.to/yashsugandh/creating-files-and-directories-in-linux-system-1b2d)**, **[deletion](https://dev.to/yashsugandh/deleting-files-and-directories-in-linux-system-4hbf)**, and **[manipulation]((https://dev.to/yashsugandh/how-to-copy-move-and-rename-files-and-directories-in-linux-system-4kpo))** 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

![locate file syntax](https://dev-to-uploads.s3.amazonaws.com/i/9ju6eljgxqtvcwj0x39w.png)

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

![locate-single-file](https://dev-to-uploads.s3.amazonaws.com/i/unm67e1uo7yeto7nfl57.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/g4ewaqltkhccfllpepvt.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/pqjektilnst2rxxzqw4j.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/xpup7k6dzefncw2un6pr.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/0hsbzjgl2n6crae1v9jj.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/trw6uh9oi0h51czwc6b8.png)

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.