Iafzal: Difference between find and locate command.
Now, we recently learned how we could use find
and locate commands to search for files
that are usually forgotten by system administrators
once they are created. (Iafzal laughs)
So in this lesson, we will learn the exact difference
between find and locate commands.
Okay, so locate command uses a prebuilt database,
which should be regularly updated,
while find command, rely on a file system
to search for files.
Thus, locate command is much faster than find command,
but can be inaccurate if its database is not updated.
So keep that in mind, it is very important.
So for you to have that database updated
whenever new files are created,
then you have to run the command, updatedb.
Now, if you do not run this command,
then by default the operating system will run
or update the locate command database itself
after a few hours of creating the files.
Now, of course, it depends on you,
if you do not want to wait
and you wanted to get the right results,
then of course run the command, updatedb manually
right after creating files or directories.
Now without further ado, let's get into our Linux machine
and I will show you exactly what is the difference
between both of these commands
that we learned in the previous lessons.
I'll go straight into my Linux machine.
I'm logged in as myself,
and of course my host name is MyFirstLinuxVM.
Okay, so now if I create a file,
let's say touch and let's call it susan.
Hit Enter and now where am I,
which territory am I in?
So you do pwd command.
Okay, now this tells you I am in
Iafzal Home Directory, beautiful.
Now, if I wanted to find a file,
I would run the command find space dot,
meaning this current location where I'm in,
-: name susan, and hit Enter.
And now it's telling you the susan file
in this directory, right?
You could also do the same thing if you could specify
the fully path, a fully qualified path, absolute path,
which is /home/iafzal and then you hit Enter.
Okay, then it will tell you
that this Susan file is located
in /home/iafzal directory.
If you run the locate command on Susan,
you think, "What will happen here?"
Well, let's find out.
Why, because the locate command relies on its own database,
a local database, and that database
is not updated yet, right?
So for that database to know,
"Hey, there is a new file
that has just been created and it's called susan,"
you need to know about it
and you need to tell the database about it.
So then what you need to do is we need
to run the command updatedb.
When you run that, you will get that error message saying,
"Updatedb cannot open a temporary file
for var/lib/mlocate/mlocate.db."
By the way, this is the actual database file
where all the file's information on the systems are stored.
So it's saying it cannot update because,
or it cannot find it or cannot open it,
because we are not a root.
So to become root and run updatedb,
you do switch to root and run the command updatedb.
Okay, it has completed.
Now exit out of root.
Make sure now you are yourself.
Now you run the command, locate on susan and hit Enter,
and there you go.
It finds it right away.
It finds it so fast.
You do not have to specify the path
where it has to go and look for.
You do not have to specify any option, dash, name,
or anything, it's just that simple.
You will run a locate command followed by
the name of the file or directory that you're trying to find
or search for, and it will find it in a split
of a second for you.
That's why locate command search is a lot faster.
However, again, it relies on that database accuracy.
So make sure every time you create a file,
you run updatedb command if you want
that locate command to find that file right away.
So that's the main difference that I wanted
to cover in this lesson.
I know I have a lot of people,
a lot of students ask me about this question,
find and locate command.
So I added this lesson for you guys,
so there is no confusion about it now.
And now you know everything (Iafzal laughs)
about these two commands.
All right, enjoy.