Location Commands (Demo)

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or

Already have an account? Sign In »

Time
21 hours 25 minutes
Difficulty
Intermediate
CEU/CPE
21
Video Transcription
00:00
>> Hey Cybrarians.
00:00
>> Welcome back to the Linux+ course here at Cybrary.
00:00
>> I'm your instructor Rob Goelz.
00:00
In today's lesson,
00:00
we're going to learn about Location Commands.
00:00
Upon completion of today's lesson,
00:00
you're going to be able to determine the tool to use,
00:00
the best tool to use to locate a file or directory.
00:00
We're going to use tools like find,
00:00
locate, and which and where is to do some searching.
00:00
Let's go ahead and find some files with some demo time.
00:00
Here we are over in our demo environment.
00:00
The first command we're going to start
00:00
with today is the find command.
00:00
Now, find is probably
00:00
the most versatile location
00:00
or search command that you're going to use.
00:00
The general syntax in find is that you use find with
00:00
a directory that you want to search and
00:00
any options that you want to include in your search.
00:00
Then you actually put in the search parameter
00:00
here that you want to use.
00:00
Those options that you
00:00
see there are really what make it so versatile.
00:00
For instance, if we want to find
00:00
a file by name in the home directory,
00:00
we can do find,
00:00
[NOISE] home, name and we'll name it as file 1.
00:00
[NOISE] We can see that it actually
00:00
finds that file one in my home directory that we created.
00:00
We also get a permission denied.
00:00
That's because I'm running this as me.
00:00
A lot of times with the find command,
00:00
you're going to want to run it as pseudo
00:00
so that you can see everything.
00:00
In fact, we'll do that right now.
00:00
Let's go ahead and find a file by type.
00:00
Let's just return our list of all the directories,
00:00
that we can find in the home directory.
00:00
Let's do that through
00:00
pseudo so that we can make sure that we
00:00
can get into all of the different sub-directories here.
00:00
We'll do find in the home directory.
00:00
Now, we're going to specify type.
00:00
We'll say that we want to specify type d for directory.
00:00
Now when we hit "Enter," we'll type in
00:00
my pseudo password to become root temporarily.
00:00
Now, we can see all the directories,
00:00
not just the directories that are under my user,
00:00
but also the directories that are under test.
00:00
That's why sometimes when you're using the find command,
00:00
you're going to want to elevate privileges with
00:00
pseudo and temporarily become root.
00:00
Now where the find command really shines
00:00
is when you're searching for files by size.
00:00
On CentOS, we can search
00:00
the home directory for
00:00
files that are larger than one meg.
00:00
We can do that by doing find.
00:00
Then specifying when doing in
00:00
the home directory again here.
00:00
[NOISE] Now, we're going to use the size option.
00:00
What we're going to tell it for our
00:00
search parameters are going to be we want
00:00
any files that are larger than one mega in size.
00:00
We hit "Enter."
00:00
Actually, let's do this as pseudo as well,
00:00
just so we can get in everything.
00:00
Now, we hit "Enter" and we can see that there's
00:00
a few files that are pretty large on the system.
00:00
We can also do this for the entire file system.
00:00
For instance, instead of being in home, we can say root.
00:00
Let's do this for a little bit larger files,
00:00
so we don't get a ton of entries.
00:00
Let's do this for anything that's larger than
00:00
300 megs in the root file system on this system.
00:00
This will take a little while,
00:00
but let's go ahead and run it.
00:00
We'll see it's going through a few things.
00:00
There's still a few things we can't get
00:00
into, permission denied.
00:00
But if we look and see,
00:00
we only have one really big file.
00:00
Then is because we have a package downloaded in
00:00
RPM package for Java that's on the system.
00:00
We can verify that.
00:00
We can actually copy and highlight this entire line.
00:00
We do an lS-ALH on that [NOISE] and paste that in.
00:00
We can see that that is 324 megs in size.
00:00
We can see that our command work correctly.
00:00
Now, the locate command
00:00
is the next command we're going to cover.
00:00
This is also very useful to
00:00
find or locate files on the system.
00:00
Particularly if you can't keep track of all those
00:00
different find options we just talked about.
00:00
[LAUGHTER] I use locate allow whenever I'm like,
00:00
darn it, how do I do this and find.
00:00
I don't know, let's just use locate.
00:00
However, the problem is locate is not on every system.
00:00
You should always check and make sure it's there with
00:00
the which command and the which command is one of
00:00
those we're going to cover in this lesson.
00:00
But for our purposes, we're just going to
00:00
run which, locate.
00:00
[NOISE] If locate is installed,
00:00
we'll see which returns something like we see here.
00:00
User has been locate, that means,
00:00
the locate file exists.
00:00
It may not be installed though,
00:00
so if it's not installed like I don't
00:00
think it's installed by default on a [inaudible],
00:00
you may have to go ahead and install it.
00:00
But once you've installed
00:00
locate or really anytime you're using it,
00:00
the best thing to do first and foremost is to
00:00
update the database that it relies upon.
00:00
Now, you need to be pseudo,
00:00
you need to use pseudo or become root to do this.
00:00
Then the command that you use to update
00:00
the location database is called update DB.
00:00
[NOISE] What this does is it just
00:00
runs and it updates location database.
00:00
Doesn't tell you anything, but as
00:00
long as it returns with no errors,
00:00
you're good to go, you're good to proceed.
00:00
Now, we can find the file.
00:00
Let's find the same file we were looking for before,
00:00
we'll just do a locate on file one.
00:00
Notice that we're not telling it, hey,
00:00
look in the home directory or we're not telling
00:00
it the file's type or any of that stuff.
00:00
We just say, hey, I want to locate this file
00:00
and we hit "Enter" [NOISE] and it
00:00
returns the same information.
00:00
Locate is very helpful if it's installed,
00:00
but don't rely upon it being installed in every system.
00:00
Always run the which command
00:00
to make sure it's there first.
00:00
That brings us to that command, the which command.
00:00
The which command is used to determine
00:00
the location of a particular command or program.
00:00
I use a lot for what we just saw in
00:00
verifying a program exists on a system.
00:00
It's also really helpful to see
00:00
the full or absolute path to a program.
00:00
When we run which locate,
00:00
we see that it's actually user bin locate.
00:00
That's the full path to the binary,
00:00
to the program on the system.
00:00
It's always good practice to use
00:00
that absolute path to a program if you're not sure.
00:00
They may not be in the path.
00:00
Not normally user bin isn't passed on every Linux system,
00:00
but some commands may not be.
00:00
You may actually have to use the whole path to it.
00:00
If you're running a script, if you're writing a script,
00:00
sometimes the script doesn't have
00:00
all the same path variables that you do.
00:00
It may not know where to find locate.
00:00
When you're writing a script,
00:00
you're going to want to use the full and absolute path
00:00
through the command that you want to run.
00:00
We'll cover more about writing
00:00
scripts later in Modules 25.
00:00
The next command we're going to look at is the
00:00
where is command and the where is
00:00
command's going to show you a little bit
00:00
more information than what we saw.
00:00
[NOISE] Really, it's going to ideally show
00:00
you information about things like the absolute path,
00:00
as well as some documentation and other things.
00:00
The where is command is just going to show
00:00
a lot more information
00:00
about a particular program or command.
00:00
We'll use locate again as an example.
00:00
[NOISE] We see now it doesn't how us user bin locate,
00:00
it also shows us the main page path to it.
00:00
In some cases, this command will
00:00
also show the location of
00:00
the source files that are used to install the command,
00:00
but the cool thing about where is that we can use it
00:00
with options to say exactly what we're looking for.
00:00
You can say, hey, where is, we just want to know about
00:00
the binary and we tell it that with the be option.
00:00
We just want to know where the binary is for locate
00:00
and we hit "Enter" and it tells us
00:00
that in user bin locate.
00:00
B for binary and binary
00:00
and Linux is just another word for the program.
00:00
The program locate lives at user had bin locate.
00:00
We can tell where is that.
00:00
We just want to know about the documentation
00:00
by using the m flag,
00:00
m for manual and
00:00
it's going to return the path to the manual,
00:00
the main page that is there for locate.
00:00
With that, we've reached the end of this lesson.
00:00
In this lesson, we covered locating files
00:00
in directories using the commands find,
00:00
locate, which, and where is.
00:00
We also use the update DB command
00:00
to update the location database.
00:00
Thank you so much for being here and I look
00:00
forward to seeing you in our next lesson.
Up Next