File Readers (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 Cyberians. Welcome back to
00:00
the Linux plus course here at Cybrary.
00:00
I'm your instructor Rob Gels.
00:00
In today's lesson, we're going to discuss file readers.
00:00
Upon completion of today's lesson,
00:00
you're going to understand the commands and
00:00
utilities that we could use in Linux to read a file.
00:00
We're going to work with some of
00:00
these tools such as grep,
00:00
cat, tail, head, less and more.
00:00
Brief terminology talk here.
00:00
Grep is actually short for
00:00
the GNU regular expression parser.
00:00
It's used to search for text in a file and it returns
00:00
any lines that contain
00:00
that string of texts that you're searching for.
00:00
The cat command is short for concatenate.
00:00
The actual purpose of this command
00:00
is to combine files together,
00:00
but generally we just use it as a shorthand to say,
00:00
hey, cat out this file
00:00
and print it's output to the screen.
00:00
In other words, print it to standard out.
00:00
Now the tail command reads the end
00:00
of a file where head reads
00:00
the beginning of a file and less
00:00
and more are examples of something called a pager.
00:00
A pager reads one page at a time and
00:00
less came after more so the joke is that less is more.
00:00
Now let's see all of these commands in
00:00
action with some demo time.
00:00
Here we are in our demo environment
00:00
and the first thing we're going to start out with grep.
00:00
Grep is just a great tool
00:00
that you're going to use all the time.
00:00
In fact, I used it previously in
00:00
the course when we were working with users and groups.
00:00
Grep is helpful whenever you need to
00:00
find a string in characters in the file.
00:00
It just reads the file and returns lines that
00:00
are in there that contains
00:00
the string that you're searching for.
00:00
If you are grabbing for string to
00:00
search and then you type in file to search.
00:00
What it'll do is it will return
00:00
the content of this file
00:00
with a string that you're searching for.
00:00
For example, let me
00:00
go back to the beginning of the line here.
00:00
I'm going to clear this out and Control
00:00
K. That just deletes the end of the line.
00:00
I'm going to do a grep for test in Etsy password.
00:00
It's going to return any lines that
00:00
contain the word test.
00:00
We can see it returns test and it returns
00:00
the information about the test user on the system.
00:00
I can do the same thing for me and there we go.
00:00
We can see all the currencies for root.
00:00
But here notice that there's a
00:00
>> root user and there's also
00:00
>> an operator that has a root directory.
00:00
It returns more than one occurrence.
00:00
If there's more than one occurrence in a file,
00:00
it just searches for the string.
00:00
We're going to cover grep in
00:00
a lot more detail later so stay tuned for that.
00:00
Let's move on to the cat command.
00:00
As I said, the cat command is
00:00
>> generally best used just to
00:00
>> printout the shortest file you can find to the screen.
00:00
You don't want to print out long file.
00:00
For instance, we're going to print out cat,
00:00
Etsy host and that's all good.
00:00
But if we were trying do, let's say var log messages.
00:00
Sudo cat var log messages.
00:00
That's going to fill up our whole dang screen.
00:00
There are much better commands to use
00:00
that and pagers are
00:00
things that you could use to use that,
00:00
they are much more efficient.
00:00
But the actual purpose of the cat command that I spoke
00:00
about earlier is to combine two files together.
00:00
Let's go to my home directory.
00:00
I have a couple of files that we can look at here.
00:00
You have cat, let's take a look at file 1 and file 2.
00:00
This is file 1. This is from file.
00:00
If we type in cat file 1 space file 2,
00:00
it combines the two of those file together.
00:00
Just put stat output together into the screen to
00:00
standard out or we can redirect it
00:00
to a file which we'll talk about later.
00:00
An example of using cat ness way would be to
00:00
create a certificate chain with engine X that
00:00
combines a signed cert in an intermediate cert into
00:00
one file and that's used for SSL or TLS certificates.
00:00
That's way beyond the scope of this course,
00:00
but that's just a good real-world example
00:00
of when you would use cat in this way.
00:00
Cat has some cool options.
00:00
We could do cat-n on Etsy password for instance.
00:00
This is going to display all the line numbers on
00:00
the left-hand side so you can
00:00
see each and every line number.
00:00
If you say, hey dude, I think there's something
00:00
wrong with Etsy password.
00:00
Can you look at line 13 on
00:00
the nobody line and tell me if that looks right to you.
00:00
That's a really good use for that.
00:00
You could also do cat-a and you can see hidden files.
00:00
Let me clear the screen and I'll just run
00:00
this on file 1 in my home directory.
00:00
What we can see at the end of
00:00
the old files is this dollar sign.
00:00
What is that about? Well, if you get rid of
00:00
a we see no dollar sign.
00:00
What this does, this shows all hidden characters.
00:00
End of line, tab,
00:00
and control characters in Linux.
00:00
That dollar sign indicates the end
00:00
>> of the line character.
00:00
>> This is good if you're working with
00:00
files that came from a different operating system
00:00
because sometimes they don't have
00:00
>> the same characters for
00:00
>> things like tabs and control characters
00:00
underline and can cause problems.
00:00
If think there's something weird
00:00
with the file is not quite
00:00
working properly with commands
00:00
and links you are trying to run on it,
00:00
check it out with cat-a and make sure it
00:00
has the proper characters in place.
00:00
Now let's move on to our head and tail commands.
00:00
These are just neat and pretty easy to use.
00:00
Basically head, you just run
00:00
head and then the file name that you want to look at.
00:00
That will give you the first 10 lines whereas
00:00
the tail command will give you
00:00
the last 10 lines of any file.
00:00
But both commands actually can use the option dash n,
00:00
display a number of lines specified.
00:00
You do it head-n of 15 on
00:00
Etsy password and then it'll display
00:00
the first 15 lines of Etsy password.
00:00
>> Or we could do tail.
00:00
>> We could do tail 20 Etsy password
00:00
and it'll split the last 20 lines.
00:00
But you might be saying, wait Rob,
00:00
where's the dash n option?
00:00
Well, here's the cool thing about head and tail.
00:00
It will take n as input a number.
00:00
You just give it a dash number and it knows that,
00:00
that just means read that number of lines.
00:00
You don't actually have to specify the n option,
00:00
but you can do it as well.
00:00
A tail come in has
00:00
one other really interesting option that
00:00
I frequently use and that's the follow command.
00:00
For instance, if we wanted to follow
00:00
the output of a file is going in real-time,
00:00
we could do tail-f on var log messages.
00:00
We'll do this with sudo so it doesn't give us problems.
00:00
What we can see here hopefully is that
00:00
any things that get written,
00:00
you could see that it updates.
00:00
It updates in real time
00:00
anything that is getting into the file.
00:00
This is really helpful if you're trying to track
00:00
down a brute force attack happening on your system,
00:00
trying to see what's going on.
00:00
Maybe taking a look at if you're adding a kernel to
00:00
the system and you want to watch
00:00
the message in real time.
00:00
This is all good reasons that you might use to
00:00
tail-f option to follow the file in real-time.
00:00
But we'll hit "Control C" and get out of that.
00:00
Let's move onto our pager commands.
00:00
Now I'm actually not going to show you the more pager
00:00
because the less pager is better than more.
00:00
Less has more capabilities than more.
00:00
The joke is that less is more.
00:00
These are known as pagers.
00:00
They display page output one file at a time,
00:00
one page at a time rather and the less command
00:00
is used instead of the more command
00:00
because it's more feature-rich.
00:00
You can move backwards with less whereas with more
00:00
you can only move forward in
00:00
a line and you can move one page at a time.
00:00
Also with less you can search using
00:00
the forward slash and the question mark characters.
00:00
Let's see this in action. Let's go and do
00:00
less Etsy password and
00:00
then we can just see all the output of this.
00:00
This isn't a very big file,
00:00
so I can go down by hitting
00:00
page down but it doesn't really do anything.
00:00
This is not a large file. Or you can go back up.
00:00
Let's do less on our var log messages again,
00:00
that example that we had.
00:00
Earlier were like, wow, this thing is huge.
00:00
This one we need to be sudo force while we need to
00:00
have root access essentially to see it.
00:00
We can run that and then we can use the page
00:00
down commands to move down the page.
00:00
Or we can use the page up commands to
00:00
move up one page at a time.
00:00
There are also some cool shortcuts unless you can
00:00
use capital G to move to the very bottom of the file.
00:00
Or you can use double g,
00:00
lowercase to move to the very top of the file.
00:00
Let me get out of this.
00:00
The way to exit less is by hitting Q on your keyboard.
00:00
Q is for quit and let me go back into
00:00
Etsy password that we were looking at
00:00
before because I want to
00:00
>> show you how you search in here.
00:00
>> If you're trying to search forward in less you hit
00:00
the forward slash button
00:00
and it type what you're searching for.
00:00
I'll just try and find myself in this file.
00:00
Then when I hit Enter we'll see
00:00
that it actually highlights my name.
00:00
Now, let's move backwards.
00:00
Let's search in reverse.
00:00
We can search backwards by using
00:00
the question mark character and then just
00:00
typing what we're trying to search for.
00:00
I'm going to search for root and we'll see
00:00
that it returns this operator we saw before.
00:00
If I hit lowercase n,
00:00
it's going to display the next place
00:00
that it finds the search value
00:00
>> that I'm looking for root.
00:00
>> Then I can hit N again and it tells me
00:00
the pattern is not found which means that those
00:00
are the only two places that is in
00:00
this file is the only lines where the word root occurs.
00:00
We can also hit dash and capital
00:00
N and then hit "Enter"
00:00
and it will display the line numbers.
00:00
This is just like what we saw when we ran cat-n,
00:00
is that this is how you display
00:00
>> the line numbers in less.
00:00
>> Then again with less, you can just hit
00:00
quit by hitting Q to get out of the file.
00:00
In this lesson, we covered reading
00:00
files using the utilities grep,
00:00
cat, tail, head,
00:00
less, but not more because less is more.
00:00
Thanks so much for being here and I look
00:00
forward to seeing you in our next lesson.
Up Next