The beautiful people.
Now, in the last few lectures you've been doing so much stuff with files you've been, you've learned
how to create them, move them around, copy them, delete them, paste them, rename them and search
for them, and so much more, all from the command line.
But even though we can now manipulate files on a grand level, we haven't really seen how to take a
quick peek at what's inside them and manipulate their contents yet.
Well, that's what these next few videos are for.
Over the next few videos, you're going to be learning five new commands that will allow you to manipulate
file contents.
So this is stuff such as reading files, reversing files, and even sticking files together, as well
as reading them in some paging programs.
So what this is going to allow you to do is to get most of your work done from the command line without
ever having to need to open up external editors, which is going to be a massive productivity boost,
and it's going to help you keep you inside the whole command line work process ecosystem.
So this is going to be very, very useful.
It's kind of like a missing piece at the minute.
And after it all, after the end of the next few videos, you're going to be out to perform a whole
bunch of useful functions on the contents of files, which, when combined with what you know about
manipulating files in their entirety, is going to make you the file manipulation master.
So let's just go ahead and jump right into it.
Okay.
So the first command that we're going to take a look at is the cat command.
Now, do you remember those kitten jokes that I was talking to you about earlier?
Well, meow.
So now you've actually seen the cat command before.
So this will be a rather nice refresher.
Okay.
So you can see here on our desktop, we've got a few files.
We've got far one file two, file three, file four and file five dot txt.
So let's take a look at what's inside the first file.
But instead of opening it, we're going to look in our terminal to see what's inside the file.
Okay.
So if we type cat and then file.
Oh, I need to be on the desktop, don't I?
So let me change the desktop.
I click the screen and then type cat and file one dot txt.
I'm going to press enter.
We'll see that the contents of the file have been written to standard output and we can see that the
file contains the word hello.
Now if I press if I double click that and open that graphically, we can see that.
Yes.
All that file contains is the word hello.
Okay.
So using the cat command, we managed to get that the contents of the file printed to standard output.
Okay.
So let's try and see what's in file number two.
Okay.
So if we do that file to txt, we can see the word there is inside.
Hmm.
I guess I guess you can imagine where this is going.
Right.
So if I go file three, it says you file.
I see file four.
It says beautiful.
And I bet we can all guess what's in file number five.
So if I press five, it says people.
Okay, so if you put all the five files together, it says hello there, you beautiful people.
Well, what a coincidence.
I wonder how that happened.
So, in fact, you can actually use the cat command to stick files together.
And that's actually usually what it's for.
Cat is short for the word concatenate, which is a fancy word for stick together.
So what cat does is stick together all of the input files and then pass that that stick together stuff
to standard output.
Okay.
So if we do, if I click the screen, if we do cat file one txt file to txt file three point text file
for txt and file five dot txt and press enter.
You can see that.
Hello there you beautiful people has been made by sticking together the five different files and then
that has been printed to our standard output.
But we can of course redirect this to a new file called Beautiful txt on our desktop, which we see
has been created here.
And when we open that, we now see that we've got a new file that says Hello, are you beautiful people
that has been created by sticking together five other files.
Now we can actually you can see this command line here is quite long, right?
Well, we can actually simplify that using our knowledge of wild cards.
So instead of having cat file 1.2, see file 2.2 key file three point text file 4.2 and 55. txt.
You can just use a wild card like this cat together files 1 to 5 dot txt into beautiful.
So if I if I delete beautiful that text and run that wild card and it's been created again and we can
see here that yep we get exactly the same result.
Hello there, you beautiful people, but a lot less typing.
So that's pretty cool.
Right now the cat command, when you want to stick files together like this, it's useful for sticking
together text files, obviously, but it can be very useful when working with audio or MP3 files, for
example.
So you could have multiple different single MP3 files, you know, different songs, for example, but
you can actually concatenate them together, stick these different files together to create one long
file that has all the music in it.
So that's pretty epic.
So the cat command is used to stick files together, or actually if you want, just read the contents
of a file so we could just do.
Cat beautiful txt so.
Cat beautiful txt and we can see the contents of any of any file which is really, really useful.
But another command that I want to take a look at is the tac command.
So let me just clear the screen here.
So the tac command is just cat spelled backwards and it's used to reverse whatever it receives as an
input.
So if we make a file called Alpha txt by echoing let's say ABC to Alpha txt and we're going to also
echo d e f to alpha and notice the way that we've got two arrows here because we're going to append
to the same file rather than overwrite it.
Okay.
So now if I look in alpha txt we've got ABC on one line and def on another.
So if we read the file, we read the file using cat we sue cat alpha txt we get abc then d e f okay,
but let's see what happens if we read it using tac.
So instead of cat, we read it using tac, which is the cat spelled backwards.
And you see now that we get def then ABC.
So you can see that the tac command actually reads the whole file in reverse but leaves the file the
lines the same.
So it just wrote the last line first and then the first line last.
Right?
But it didn't affect any text on each line.
So if we stick together the various different files here, the file 1 to 5 using cat.
So if we do file 1 to 5 dot txt, you see that we get the output there.
But now I want you to think what might happen if we pipe that into TAC?
Now TAC will reverse the file line wise.
So the last line will be printed first and the first line will be printed last.
So what do you think might come out of this?
Just take a couple of seconds.
Ever think about it?
So what might happen is the last line, which is people would come first and the first line would come
last.
So what might come out is people beautiful?
You there?
Hello?
Okay, so let's have a look.
There we go.
Yeah, people beautiful.
You there?
Hello.
So think of the tag command as reversing a file vertically.
Okay, so it just flips it upside down.
It doesn't affect any of the lines, it just flips it upside down.
And of course, because this is all being printed, the standard output, you can of course redirect
it into another file called reversed tcd and here we are.
Here is reverse text.
When we open that, we see that we've saved the output in that file.
So that one good thing about the tac command where it can be used is again, if we take the example
of MP three files, I don't know if you've ever seen those videos online where people say, Oh, look
what this song says when it's played backwards.
And like, you know, they tried to say it's got some hidden messages in the file.
Maybe you've seen those kind of videos.
Well, maybe they were made using the tag command because if you run an MP three file through through
the command commands, if I did something like tack my file the MP three and save that into another
my reversed file dot MP three then this file, this one here will be the same song but played backwards.
So that's something to to potentially give a go if, if, if you would like.
Okay, so that's the tac command.
The tac command will reverse files vertically.
There's one other that I want to look at, which is called the Rev Command.
So the Rev Command allows you to reverse the content on each line.
So if I clear the screen and we cut together the various different files, again we get hello there,
you beautiful people.
But if I pipe that into the rev command, look what happens here.
It's still saying if you can read it, it still says hello there, you beautiful people.
So the lines are vertically the same, but horizontally they've been reversed.
So the last letter of each line.
So here's the o has is now become the first letter and you know it's reversed them along the lines but
the order of each line is the same, if that makes sense.
If we if we put the the tac command, you can see that people is the first line, whereas here people
is the last line.
But the letters have been reversed.
So this is the way you can think of rev and tack.
Tack reverses a file vertically and rev reverses a file horizontally.
Okay.
Now you can really jumble up files by piping it through both of them.
So if I pipe it through tack, it's going to reverse vertically.
But then if I pipe that also through rev, you've now got a vertically reversed and horizontally reversed
file, which is a good way of jumbling up stuff if you ever find that particularly useful.
But yeah, just to make it easier, just think of tack as reversing files vertically in columns and
you can think of Rev as reversing files horizontally across the rows.
So all of these commands, whether it be cat or it be tack or it be rev, all they're doing is they
are taking a file and reading it into standard input, doing some processing and then, you know, just
spitting that the output to standard output.
Okay.
So they're a good way.
Well, cat is a good way of reading files and you know, the others are a good way of jumbling up stuff.
But the problem is when you read files to the terminal like this, if a file is particularly long,
it can really just clog up your screen and it's not very useful and you find yourself scrolling a long,
long way.
So if we try to find a file that's quite large, so if we say, for example, let's use the find command,
let's search our system up to, let's say a maximum depth of four.
We don't need to go too far for any configuration file.
So a name that ends in com and a size that let's say over 20 kilobytes so we are we only get we only
get two really.
But there's one here in called cups browser.
And this is to do with like printing services and it's apparently a, it's a rather large file.
So if I copy it, copy that and try to cut it out.
Okay.
So if I try to cap that, oops, you see, you see it just happen right there.
If I tried to cap that out and press enter, you see that we've just shut down a stupid amount of lines.
Can you see?
Now, if I wanted to have a look at it, I ended up scrolling all the way through here.
It's not very good, right?
So that's a problem when you're dealing with stuff on the command line.
And we want a more elegant solution to dealing with relatively large files.
And sometimes, you know, if I if I just wanted to read out of how maybe I don't want all of the file,
maybe I only want a certain snippet of it.
Maybe I only want the top ten lines or the bottom ten lines or something like that.
And we need to have a better way of being able to deal with file contents on the command line.
So in summary, so that's what we're going to be covering in the next video.
But in summary of this video.
We've seen that the cat command can be used to actually read out files.
So if I look at Beautiful on our desktop, if I read out beautiful text, we can read that out.
If we want to reverse files vertically, we can use tack.
If we want to reverse files horizontally, we can use rev.
But all either way, all of these.
What they're doing is they are just reading a file's content and spitting it out to standard output
where you can then of course pipe it into other commands to do other processing or like while you're
working on the command line, if you just want to see quickly what's in a file, you can just you can
just use cat the cat command and then see that really quickly and get on with what you were doing.
Okay.
So there we are those those are the cat tack and rev commands.
But in the next video, we're going to take a look at how you can level up this ability to do more sophisticated
things.
So I'll break the video here and see you in the next video.