[No audio]
File Display commands. This lesson covers about
all the commands that are used to display the
contents of a file. You can view the file by doing
ls -ltr, but and you could, how can you
view inside of the file. The first command that
we'll cover is the cat, which views the entire
content of a file regardless whether it's one
page or two page. more command or less command,
both are the same. But more views the content of a
file one page at a time, whereas less command
views the contents of a page in reverse order, but
it also allows you to view one line at a time.
Then you have head. If you want to know a few
lines, at the top of a file, let's say if a file has
million lines, and you just wanted to view the
first 10 lines, you could use head command,
followed by the number of lines that you want to
view. The same way the tail works, instead of the
top of the file, it actually gets you the lines at
the bottom of the file. Again, if a file has a million lines,
and you put down minus 10, it
will give you the last 10 lines of a file. Let's
look at our our Linux machine and see how we could
use these commands.
[No audio]
Okay, so let's clear this page
out. Okay, the first thing I want you to do is
copy the file called var/log/msgs, and copy it
in your home directory. You could do slash home,
your home directory, which would work or you could
also do copy the name or the location of the
entire file and followed by space and dot, dot
means at this location where I am, you hit enter.
And now you do ls -ltr, hit enter and you
will see at the bottom right here, it shows that
you have this file, which is 129338 bytes long. By
the way, you could view, you could copy this file
only if you are root. So when you do whoami, you
are root. And you remember how you will become
root. Let's say if you exit out of it, and you do
whoami, you are yourself. And now you are going
to copy var/log/msgs to your current location.
It's not going to allow you so you have to be root.
So to become root, su -, put in the
password. And now you are in root. So go to your
home directory, the user's home directory, which
is your home directory. And then you'll see here
this has this messages file that you copied from /var/log.
And the reason you were not able to copy
it because you see here, only root which is the user,
the owner of the file who is root is allowed to
read and write to this file. The rest of people
which is group has no right for anything, and so
on. The others have no right on read write to do
anything. Let's go back to our commands that we
will cover. If I wanted to view the contents of
this file called messages, the command to use is
cat messages. Simple as that, you hit Enter, see it
has all those contents, messages, basically this
messages file has all your logging information of
your system. Anything that is going on with your
system, any services that are running any error
messages, warning messages, coming in the health of
your computer, everything is stored in this file.
We copied this file in our home directory
just to cover the file display command lesson. So
do clear, and then do cat and messages again, and
you'll see it has a bunch of lines, text lines in
it, and we could view through cat, but then we have
to scroll up. So another way so that we don't have
to scroll up, how do you do, you do more messages.
You hit enter, and there you will see, you will get
one page at a time of that file messages. Here
you'll see it says 1% at the bottom, you hit
spacebar to keep going down, keep going down, you're
going one page at a time. And while you're going
it also telling you the percentage of the file you
have read. You keep going down, and if you want to
get your prompt back, what do you do, you simply type Q.
So that's how you use the more command. Now let's
use the less command. less command space messages,
hit enter, it does the same thing, you hit the
spacebar, hit the spacebar one page at a time.
Also, it will give you one line at a time to view
it, when you hit j in your keyboard, it will come
one line at a time, or you hit k, which is a go
back up. You could also do up arrow key or down
arrow key, whichever that you prefer j or k, up arrow
key, down arrow key, whichever you prefer. So that's
what less allows you to do. Next one, we have
head. If you do more, and messages, you'll see the
first page of your file messages. What if you want
to see only the first two lines, you don't care
what's inside the file after the first two lines.
Then what do you do, you do head -2 space,
the name of the file, messages, and you will see
the first 2 lines of the file. Then you'll do
tail -2 messages. This will give you the
last two lines of the file, which is the last who
is March 7th, 3:10. You could do cat again to
messages and we'll bring you down to the last
page, and you can verify yes, these were the last
ones. So to wrap up, we covered cat command which
lists the contents of your file regardless how
many pages it has, this just displays everything
in one shot. more gives you one page at a time. less
give you one page at a time in reverse order but
also lets you view one line at a time. head
gives you the first lines depending on what you
define, and tail gives you the last lines depending on
what you have defined. Now exit out of root
account, find out which directory you are in, and
do ls -ltr. And you will see there is a file
called jerry in there. Let's do cat and jerry. And
you will see two lines in jerry file. The first
line says, Jerry seinfeld is the main character, and
the second line shows us the date and time of
file, not the file but an output or a text
message, whatever that is. If I just wanted to
view the first line of the file, I don't care the
second line, what I would do is head -1
jerry and it will give me the first file first
line of the file. What if I want to view the last
line, I don't care the first one that says Jerry
seinfeld is the main character. I just wanted to know
this line. I will do tail -1 jerry, and it will give me
the last line. So this is how head and tail work.