find and locate command in Linux.

This is just a basic article covering the basic use of find and locate command.

Let's say you created a file something about 6 months or a year back and you forgot about that file and one day suddenly you realized that you need that file. Now what to do ??

So Linux provides you with 2 commands that we can used to find files and directories. These are

  1. find
  2. locate*


find command is used to find/locate the file or directory in Linux.

syntax: find [from where to start searching] [expression] what to find .

find . -name  "royal"

-> . here symbolizes start from current directory. -> -name tells that we are gonna provide the name of file/directory to find.


Now locate does the same work as find does.

syntax : locate [filename]

Now question arises if find and locate does the same work then what's the difference between both and which one to use.

Difference between find and locate command.

So, locate uses prebuilt database which should be regularly updated. And find iterates over the filesystem to locate files.

what does that mean ????

So Basically locate has its inbuilt databases which should be regularly updated otherwise locate may not able to locate your file/directory.

how to update db ??

Simple, just run updatedb command and it will get updated.

Anshul, Can i see where this db is located ?

yaa, sure.

you can find this db inside var/lib/mlocate/ directory where you will find a file name "mlocate.db".

Now coming back to find vs locate.

So locate is much faster that find but can be inaccurate if the database is not updated. But this is not a case in find since find iterates over filesystem.

create a file and name anything you want and now run the following command

find . -name  "filename"

You will see the path where file located.

Now type the following command.

locate "filename"

It will print nothing since database is not updated yet. So run the "updatedb" command and Now again run

locate "filename"

Now you will see the output/path where file is located.

I hope you got clear understanding of find and locate command. For more details you can read more about them using man command in Linux which will give you brief idea and understanding of both of these commands.

Thank for reading and if you find anything wrong/incorrect do let me know in comments.