Hello.
The beautiful people know in the last video you learned about the different ways that commands can handle
input and output.
And I see that in your mind the concepts behind the most powerful abilities that the Linux operating
system can give us.
Piping and redirection.
Now, piping and redirection allow us to create sophisticated command pipelines and move data around
our computer system with such unbelievable ease and flexibility that it's basically a super power.
So in this video, you're going to begin gaming superpowers by learning how to perform redirection in
Linux and later on in the next few videos will be covering piping.
But by the end of this video, you'll be able to redirect standard input, standard output and standard
error to your heart's content, and you'll feel much more like a computer genius already.
So let's go ahead and get right into it.
Okay.
So our main friend for this video is going to be the cat command that we saw a bit earlier.
Now, the cat command is used to concatenate or stick together multiple different files.
You'll see how it works in more detail in a later video in the course.
But the reason I'm using the cat command in this video is because it's a simple command that reads from
standard input and writes to standard output.
So it fits our purposes of learning redirection perfectly.
So let's watch cat receive data from standard input.
So if I just type cat here, the cat command waits to receive some data on standard input.
Now cat needs standard input in order to run, but because standard input is by default connected to
the keyboard, cat just sits there and waits for us to enter something on the keyboard.
So if I type meow like that now once we do it will output the same input into standard output.
And because standard output is by default connected to our terminal, it it outputs that the same word
that we entered which was meow, which is appropriate for the cat command.
Of course it outputs that to the terminal.
Now in order to tell the cat command that standard input has finished, we need to press control and
see and cat will stop listening and we will have back control of our terminal, of our shell.
Okay, so that's awesome.
So what if I wanted to change where the standard output goes instead of having it come to the terminal?
In other words, how can I redirect standard output?
Well, let's say that I wanted to redirect standard output to a file called output text.
To do that, I would use a special symbol.
I would write cat just like normal.
Then I would type the number one and the greater than sine and then I type output text.
So let me explain to you what this means.
I want you to remember that standard output is the name of the stream.
The output goes down, standard output isn't the destination, but it's the name of the stream or the
pipe or the yeah, or the stream that the data goes down.
Now every data stream not only has a name like standard output, standard input or standard error,
but it also has a number associated with it.
Standard input is number zero.
Standard output is number one and standard error is number two.
Okay.
So what we're doing here is we're changing the destination of where stream number one points.
In other words, we are redirecting or changing the destination of standard output because standard
output is number one.
So that's why we type the number one.
And we said put the number one, put data stream number one in output text.
So instead of pointing the standard output stream at the terminal, which is where it points by default,
we're pointing it towards a file called output text in our home folder.
Now when I run, that cat is still waiting for some input to run.
So if I type Linux is amazing.
Okay.
And press enter.
We notice now that actually it didn't come back out to the terminal and when I tell cat that done an
input is finished again by pressing control.
And see, first thing we notice is that the command is no longer running and as well we also don't see
any output.
But if I click here to navigate to our home folder, so I click files and I when that window opens up,
if I never get to our home folder, we now see a folder, a file called output txt and inside output
txt.
If I open it we see the text.
Linux is amazing.
How cool is that now because standard output is usually what you want to redirect.
Linux makes it so that you don't actually even need to put the number one.
So if I just get rid of if I just delete output txt, i just press the delete key on my keyboard.
Yeah.
You don't actually even need to put the number one in this symbol here.
So actually if I just do cat then the greater than sign an output txt and I enter linux is amazing and
outputs it and then i press control and c to stop the command.
You can see that we basically get the same result.
Output txt has been created and inside there is linux is amazing.
So that's a nice shortcut.
You don't have to put the number one there, but it's useful to understand that for when you actually
come to redirect the other data streams, you might be wondering about the spacing.
So let's take the long form.
Again, the long form is cat one greater than and then output txt.
Okay, but I couldn't add it so I could write it like this.
I could get rid of the space there and that would be fine.
But I couldn't add a space between the one and the greater than okay.
So I couldn't like do that have a space in between there because this has to be all one symbol that
wouldn't work.
But this would I could have I could close the gap there.
So this symbol is preserved.
The symbol is preserved, but I can have a space after it between that and outputs.
T This is fine.
This is fine.
This is not fine.
Does that make sense?
But yeah.
Just remember that the shell is interpreting what we write.
The shell is looking for one greater than or just greater than as a symbol.
And it has to find that symbol in order to understand what it wants us.
To do so if we break up one greater than we the space now, it no longer works because the shell isn't
looking for that.
Okay, awesome.
So let's try adding more text to the file.
Let's try and add the text.
Wow.
So that it says Linux is amazing.
And then wow.
Okay, so if we do that and we type wow.
And then press control C to stop now when we check the output txt file, we expected to say linux is
amazing.
Wow.
Right.
But when we open it.
It just says the word wow.
What happened to the text that says Linux is amazing?
Well, redirection by default will remove everything in a file before writing to it.
The fancy word for that is called truncation.
But basically what happened is Linux emptied our file and then wrote wow to it.
Well, that's kind of annoying.
So how can we write to a file again using redirection without truncating it?
Well, to do that, we would write the redirection like this.
So you would write Cat one greater than greater than output.
So you can see we've used two arrows there.
So instead of having one arrow, we have to write after one another.
Now, when I type Linux is amazing and I press control and see just to end that, now we look inside
outputs.
We now have Linux is amazing after.
Wow.
Okay.
And again, the shortcut still applies.
So instead of having cat one greater than greater that by the way, that shortcut right there, I just
press the up arrow key instead of having cat one greater than greater, then I can just have greater
than greater than when I do that and I say Linux is very amazing and do that and stop.
Then when I reload the file we can see that now it adds Linux is very amazing.
So this works just as well with just the greater than greater than it worked, just as well as having
the number one because Linux makes it just that bit easier for us because usually you're redirecting
standard output, which is data stream number one.
So that's how you redirect standard output.
And this process, as I said, of using the double arrows is called appending to a file.
So in the interest of time, we're going to cut our discussion short in this video and we're going to
pick it up again in the next video.
But in summary, what you've seen in this video is, first of all, the data streams have numbers associated
with them.
Data stream number zero is standard input.
Data stream number one is standard output and data stream number two is standard error.
And you can use these numbers to actually access the specific data streams that you want for redirection.
Now, in this video, you've seen how to redirect standard output, which is data stream number one.
And it's probably the one that you will be using most of the time.
But what you need to make sure of and this works for all data streams, is you need to be careful of
truncating your files.
You saw that when you use one arrow, when you were reading, when you were redirecting such an output,
the shell would delete everything that was in the file or in the place that you were redirecting to
before adding the data in.
But if you use two arrows, then you can actually append your data to the file.
You can add your data on top of what is already there.
So just be aware of that.
So in the next video, we're going to take a look now at how you would redirect standard error and standard
input.
So I'll see you in the next video.