Hello the beautiful people.
And welcome back.
So in the last video, you learned how to open files and read their contents by using several command
line commands.
So, for example, the cat command will open up a file and allow us to read its contents.
So if we use file one dot txt, we can see that it contains the word hello.
And when we open files like this, what happens is everything is shown all at once.
So no matter how long the file is, it will all be spat out right into our terminal.
And that can be kind of a pain.
So for example, if we try to open up a really long file, such as the file that I showed you in the
last video, which is something to do with printers and stuff on your system.
So if I type that in so cops browsed dot conf and I tried to print that out, you can see that we've
just shut out a whole bunch of information and if we want to read it, we've got to scroll up and down
like this right in our command line.
And it's a bit of a pain, right, like this.
It's not really a nice way.
You want to be able to deal with long files.
So to help with this, there are programs on Linux called pager programs that allow you to page through
large output to make it easier to read.
And the one that we'll be covering in this video is called The Less Command.
So if we just do instead of cat, if we do less and then give it the file that we want to read, you
can see now that what's happening is we are we started, first of all, at the top of the file and by
just using our keyboard keys, our arrow keys, up and down, we can scroll through this content and
read it by scrolling up and down a lot easier than if we'd had it just on the command line.
And if I press Q you can see down the bottom here we got these two columns.
If I type Q, it closes and now we're back on our command line with no mess that we have to scroll through.
So that's a nice way about of doing it.
And actually you can pipe to the less command.
So instead of opening it directly like that, I could use Cat to open the file and then pipe into the
last command and it will give us the same result.
Now this ability to pipe to the less command is incredibly useful because it doesn't just have to be
files that you are that you are dealing with.
For example, if I said, okay, let's go into our into our home directory and let's find all the text
files.
Okay?
So we're going to find by name everything that ends in dot txt and we see that we get a whole bunch
of information here.
Now this could be quite long.
It could, it could be quite a lot of stuff.
But what we're going to do is we're going to instead of that, actually, instead of just a text, let's
just try and find everything, okay?
It's printed out a whole bunch of stuff that we've now got to scroll through.
Instead of that, we can just pipe it into less and now we can scroll through line by line.
So the last command is a really useful one when working with the command line, because it saves you
from having a lot of mess, filling up your screen and piping to it is is really, really, really useful
because now we can press Q And all the mess has gone.
Now sometimes you don't want the whole output or the whole file and you only want to operate on a piece
of it.
So perhaps you want to take a little piece from a file and pass it through your pipeline.
Instead of having the whole file on your screen.
You just want to peek at a little piece before getting on with your work.
So for this purpose we can use the head and the tail commands.
Now the head command will allow you to see a little piece of the file starting at the top or the head
of the file.
And the tail command will kind of do the opposite.
It will allow you to see a little piece of the file starting at the bottom or at the tail of the file.
Okay.
So for example, let's concatenate together the five files again.
So I'm going to go to our desktop and clear the screen and I'm going to cut together files.
Oops, clear the screen.
Cut together files 1 to 5.
Okay.
TXT and we see that we get five rows out.
Okay.
Now if I just pipe this into the head command.
We just get the same output.
So why is that?
Well, that's because the head command will, by default, show you the first ten lines of a file.
And since there are only five, we'll see it all.
But we can edit this behavior by specifying how many lines that we want to see.
So if I just press the opera key to get what we want, I can give the the H, the head command, the
N option and specify how many rows we want to see.
So now we're just going to see the first two lines and you'll see.
There we go.
We just see hello.
And they're the first two lines of the output.
Now the head command is very useful in pipelines.
Again, let's say we were doing the find command and we're back in our home directory and we run the
find command.
We'll get a whole bunch of output.
And let's say you just wanted the first five of these options, you could just pipe it to the head command,
give it the an option and say, Oh, hey, I just want five lines, let me press enter.
And you see, there we go.
We get the first five results and then you could take those and pipe them down, down the the pipeline
for whatever reason you wanted.
So maybe I wanted to, you know, then pass those into the tac command and reverse the order that they're
in or whatever it might be.
Okay.
So that's a really useful application of the head command.
It works the same for the tail command, but the tail command will just show you from the bottom rather
than from the top.
But I want to show you that actually you don't need to just pipe to the head command.
You can operate directly on files.
So we had that really long file, didn't we?
We had what was it slash Etsy cops cops browsed dot com.
And you see we've got this really really long file now if we can actually see how long that is by piping
that to the word count command and giving it the URL option like you've seen a bit earlier in the course,
I can see we've got 673 lines.
That's quite a few lines.
But what if we just want the first 20 lines?
Well, instead of piping it, we don't have to do that.
What we can do is we can just say head, give it the an option of the first 20 and then give it the
file and just operate directly on it.
And we will get just the first 20 lines and we can confirm that we do indeed have 20 lines by just pressing
the up arrow key here and piping that into the word count command with the option to count the amount
of lines.
And we'll see that.
Yep, we do now only have 20 lines of output, so that's perfect.
Now the tail command works basically the same way as the head command, except it starts from the bottom
of a file.
So if we do our cutting again, we go back to the desktop and we cut out file 1 to 5 dot txt and we'll
see that.
Yep.
We get hello there you beautiful people.
Now if we pipe that into the tail command and say we just want the bottom two lines, then we'll just
get beautiful people.
So the bottom two lines and if we again operated on the really, really long file, so instead of having
cat instead of having head, let's have tail.
Get rid of this word count at the end.
Now if we if we press enter, you can see that we just get the bottom 20 lines and we can again pipe
that into word count and see that.
Yes, we do indeed have 20 lines.
So head and tail allow you to cut either the top of a file or the bottom of a file, respectively,
to control exactly what you want.
And you can use these together to kind of filter out specific lines.
So let's say we did let's go back to our head example and let's say that we cut out the first 20 lines.
So these are the first 20 lines of that big long file.
And then if I pipe that into the tail command and said, okay, give me just one line from the bottom.
Okay, then this blank bit here is going to be the 20th line of the file.
Maybe that's not that exciting.
Let's say if I get say three, okay, then that's going to give me lines 18, 19 and 20 of the file.
And you can use the commands in this way to cut out specific lines.
So that's that's quite a useful combination right there.
But really where these things come into come into their real use is when you are building your pipeline.
So the example I've been using and leaning on is the find command because it gives us a lot of output.
But if you wanted to just get a specific set of lines, so let's say I wanted the last three lines,
I could just pipe those into the tail command and just say, okay, just give me the last three lines
and then continue to pipe that down or even export it, let's say, into export text on our on our desktop.
And then you can save that and keep just the lines that you want rather than having to have a whole
bunch of output that you don't need.
So it's like a filter.
The head and tail commands are best seen as filters that allow your pipelines to get you just what you
want to work with.
Q So we've covered quite a lot of stuff in the past few videos, right?
But fortunately none of it is particularly complicated, but it's all very useful.
So as a quick recap, you've seen how to join files together or concatenate files using the cat command,
which is a great tool for not only joining files together, but also allowing you to take a files content
and putting it right into standard output so that it can be used in your pipelines.
You've also seen how you can reverse files vertically, so using the tac command and you've also seen
how to reverse files horizontally across their lines.
Using the rev command and using both of those together is a real good way to jumble up your files.
Now, some files that you want to read are very, very long, and just viewing their contents with the
cat command, for example, is a bit of a mess and it gives you a lot of stuff you've got to scroll
through.
So you saw how to use the less command to page through massive amounts of output or large files from
the command line without needing to make unnecessary files just to view the output and piping from commands
to the less command is a great way to keep your terminal tidy and make your workflow more effective
and just make dealing with large amounts of data a lot easier to handle.
And finally, you saw how to use the head and tail commands to cut out just what you want from a file,
and if you would like, then pass it straight on down your pipeline.
So these are really, really useful tools that will come in handy quite often when working with file
content.
So now you not only know how to manipulate files as a whole, so copy a whole file, move a whole file,
delete a whole file.
But now you know how to operate a bit more on their contents.
But there's so much more that you can do with file contents and simply just looking at them or sticking
them together.
So in the next video, you're going to learn how you can sort a command, a files, contents, either
alphabetically or numerically.
And being able to sort contents is super useful.
It makes finding what you're looking for easier and works perfectly alongside commands such as the head
and tail commands that we've seen in this video to make really powerful combinations for getting just
what you want out of a file.
So this is going to be epic.
Sorting files is just a really, really useful ability.
So for all that goodness, I'll see you in the next video.