Hello, the beautiful people.
So in the last few videos you've been learning how to create and modify files and directories in all
kinds of different ways.
And when you're working with files in your computer, it's really easy to misplace one or two and wonder
where an important file that's new to you such a configuration file might be hiding.
So in this video, I'm going to show you how you can use the locate command to actually search for files
by name and find them no matter where they might be hiding on your computer or with the command line.
Pretty cool.
Right?
So we're going to break this up into two parts in this video.
You're going to learn how to use the locate command and some of its options.
And you're also going to appreciate some of the limitations that the locate command can have.
And then in the next video, the one after this one, you're going to learn how to overcome those limitations
and also how to access administrator privileges on the system in order to update a database.
So this is all going to be very cool stuff, very, very exciting.
It's going to give you super powers, basically.
So let's just go ahead and get right into it.
Okay.
So the locate command is actually really simple to use, but it's very, very useful.
Now, the first thing that you need to know about the locate command is that it works by searching a
database file that's on your system.
Now this database holds location information about every file that's on your system.
And the way that the locate command works is that you will give it some pattern to look for and then
locate will search the database for every path that matches that pattern and give it back to you on
standard output.
Now this is actually really easy, so let's just jump right in and get a feel for how this works.
Okay.
So for example, let's try and find every file in our system that ends in dot conf.
Okay.
Now dot conf is a file extension that is common on Linux to indicate that this file is an editable configuration
file.
And because file extensions don't really matter to Linux, Linux users and developers usually leave
meaningful file extensions like dot com so that you can understand what the file might be about just
from reading the file extension.
Dot log is another common example and it's used for log files that store error messages from programs
and server applications.
So you can look at these log files later on in case anything breaks and see where stuff went wrong.
But we're going to look at files that end in dot conf now to search for all the dot com files on our
system.
All we need to do is type the locate command and then give it the pattern we want to search for.
Now you should start remembering what we did about regular expressions and wild cards.
So just think for a minute.
How would you make a pattern that matched any file that ended in dotcom?
Just think about it for a second.
And you might remember that perhaps you need to have the star wildcard and then com.
This says anything that matches this pattern here and so that means it can start with anything and then
end in dotcom.
So this pattern will check for files that end in dot conf and that's actually all we need to do.
Right.
So if we do that and press enter.
Whoa.
Right.
You'll see, like a whole bunch of different file paths come out, but you'll see that every single
one of these files ends in dotcom.
Now, it's worth noting that by default, Linux is case sensitive, but you can make searches using
the locate command case insensitive by giving the locate command the AI option.
So, for example, if we just clear the screen and we do locate and just say dot com, but spell everything
in uppercase, now you'll see that we actually get no results because Linux searched case sensitively.
It had to be exactly everything in uppercase but to make locate search in a case insensitive way, we
could give the locate command the AI option.
And what this does is say, hey, don't worry about the case.
I'm just looking for these letters.
And when I do that we get again files that end in dot conf.
So that's something to bear in mind there.
And you can also limit the amount of results that you get.
So we get hundreds of results coming out of this one.
Look how many.
There are a lot of configuration files, but if we clear the screen, we could say, okay, let's search
in a case insensitive way, but let's limit it, limit the output to just three results.
Okay.
And you can see here that I am using the long form limit option.
So we've got the two dashes, then the word limit, and then we give it its command line argument,
which is three.
So this limit option has an input of three.
So when I do that, you can see that we are just limiting it to three results.
If I change that to five, we'll get five results for change that to ten, we will get ten results and
so on and so on.
So this is useful to if you just want to get the first few results or you only want to find a few results,
you can limit it like that as well.
Now the key consideration to have when using the locate command is to understand that it uses a database.
In fact, you can actually find out information about the database using the capital s option.
So if I click the screen and we use locate and give it the capital s the upper case option, I can press
enter and you'll see that we get some information about the database itself.
So it tells us the path to the database file as well as some size about the general, some information
about the general size and complexity of the database.
So I'm actually going to save this information using a redirect.
So if I use locate s and I'm going to save that into something on our desktop, let's call it database
before txt.
So well we've got a file called database before that txt on our desktop with all that information saved
in it.
So you can see redirection is actually a very useful skill and you'll just use it all the time and you
should be proud.
Now you understand how to use it as if it's nothing, right?
So this database, just like any of the database, is only useful if the information in it is up to
date.
Okay.
Now the database by default is updated just once a day.
So this means that anything that happens in the meantime is not logged in.
The database files could have been created, moved, deleted, and unless the database is updated,
then the locate command will start to give incorrect results.
Now there are a few options that come with the locate command to help minimize this issue.
They can't make the database list files that haven't been added to it yet because that's impossible.
But they can prevent the locate command from showing files that have been deleted in the meantime or
links that no longer work.
For example, there is the option that requires the locate command to check that the file is existing
before it reports it back.
So for example, if we did locate with the e option of everything that ends in conf, then this would
check the database for all files that ended in dotcom, but also do a check to make sure that those
files actually existed before reporting them rather than just taking it from the database directly.
It will take them from the database, but also do a check to make sure that they are there.
So it gives you that extra bit of assurance.
There is a long form option for the options.
So instead of typing dash E, you could type double dash existing and that does exactly the same thing.
So that's very, very useful stuff.
And sometimes there are shortcuts and links on your system called symbolic links that might be broken
if files get moved around now to check whether these shortcuts and links are still valid before the
locate command reports them back.
You can use the capital L or the follow option like this.
So locate follow everything that ends in dot com for this.
So if there are any symbolic links or if there are any links to files, it will check that they aren't
just dangling.
They actually arrive somewhere, arrive to the right place.
Before reporting it back.
So that's another bit of assurance.
And of course you can do both at the same time.
So existing and follow gives you basically the maximum assurance you can get there.
And again, you can combine it with case in sensitivity and limit it to just show five.
And you can combine all these options showing you here just how powerful the Linux command line can
be to have multiple different functions going at the same time.
Now, these safety features are all well and good, but what we really need to solve our problem is
to have a way to update the database whenever we like.
And there is it's called the update DB command.
And for the purposes of time, we're going to cut the video here and pick up learning how to update
the database in the next video.
So I'll see you there.