Hello, the beautiful people.
So in the last video you saw how to redirect standard output.
And in this video, we're going to be picking that up and continuing by learning how to redirect standard
error and standard input.
So let's start with standard error.
How do you go about redirecting that?
Well, standard error works in very much the same way as standard output.
Now, the number for the standard error data stream is the number two.
So to redirect standard error, you would type cat and then instead of one and then a greater than sign,
which is what we use for standard output.
You just have a two because the data stream number four, standard error is the number two.
And this would redirect the standard error from the cat command to error text and to append the standard
error to whatever is already in error text, you would do the two arrows.
So remember the difference between the one arrow and the two arrows is the one arrow will delete everything
that's in the file before append before writing to it.
So it will truncate the file and two arrows will append or add to what's already there.
So let's demonstrate this.
The cat command doesn't have the k option.
So if I do cat give it the k option and just type blah.
Right.
We're going to get an error and it's going to say invalid option k, try cat help for more information.
So this is an error message.
So therefore it was output on the standard error data stream.
And the standard error data stream is by default connected to our terminal and therefore we see it in
our terminal.
But let's redirect it to error text so I can press the up arrow key to save myself some typing and I'm
going to type the number two and then a greater than sine and say redirect that to error text.
So I'm saying run this and redirect the, the, the standard error to text.
So when I press enter, we first of all, we noticed that nothing popped out on our terminal, which
is a good sign.
And when we open up our files, we can now see there's a file called Error Text.
And when I open it, we can see that indeed the error message has been sent to that location.
A common use for this kind, for redirecting standard error is to keep track of log messages coming
from web servers and things like that.
But a common mistake is to use just one arrow like we have here, because if I keep doing that, what
that means is it will delete what's in the file before before it sends standard error again.
So if I keep doing that, okay, imagine a new error happens every time.
If I keep doing that and open error text.
Only the most recent error has been saved.
But if I instead of having one arrow, I have two arrows.
Now I'm going to append each error on top of each other.
Okay.
Which is exactly what you want with a log file, because you want to be able to scroll back and see
what's happened in the past.
So it's open it again.
Now we can see that the errors are being added on top of each other.
So that's the difference between having the one arrow and the two arrows.
The two arrows appends onto what was already there, and that's really what you need for standard error.
Now you can, of course, redirect standard error and standard output at the same time.
So for example, we could type the command and redirect our output to output text.
Remember that standard output is data stream number one.
And we could also redirect standard error to another place called error text.
And we could make sure that we've, we append both of them by making sure there's two arrows at both
places.
And then when we type something like Linux is amazing and then we cancel when we open up our file browser
here, we will see that we now have two files and output.
TXT output txt contains Linux as amazing and standard error.
The error text doesn't contain anything because there were actually no errors from this output.
It was a perfectly valid command, there were no errors required, but that is how you would go about
doing it.
So this is how we would be redirecting standard output to output tcd.
We can tell that because the number one is here and standard error to it file called error dot txt.
Now we can see what the number and the arrows that standard error would be appended and so would output
txt.
Now we actually remember you actually don't need the one output at txt, so you could actually just
do get rid of that and do the say.
Linux is very amazing and when I look at that, we're going to see output at TXT has had its text appended
and again there will still be no errors in error txt because there weren't any errors.
But you can see here that without using the number one, this will still work, but you do still need
the number two for standard error because that's shortcut isn't available.
So you can only get rid of the number one for the standard output data stream.
Okay.
Brilliant.
So you now understand how to redirect standard output and standard error.
So how about standard input?
Well, actually, that's relatively straightforward.
At the moment, the cat command is looking for standard input from the keyboard.
But what if we told it to get standard input from a file instead?
So let's make a file called input text and type in there the words Hello world.
Okay, so what we're going to do is we're going to use the cat command and we're going to output that
now to a file called input text.
Okay.
So we're going to make a file we're going to put in there Hello World.
Now when I cancel that, we have now effectively created a file called input txt.
And in there we have hello world.
Okay, so you can see how you can use redirection to create files like this as well.
Now let's use now let's try and read from that file using the cat command again.
How can we make the contents of this file, this input text file be read into standard input for the
cat command so that we don't have to type it later on, let's say.
Well, the standard input stream has the number of zero.
So what we can do is we can type cat then the number zero and instead of the greater than sine, which
is like an output, you actually use the less than sine we say then input, text or as a shortcut because
the standard input stream is the only way that you can actually input.
You don't even need the zero, you just put the less than sign right there.
Now when we do this, we see the hello world message pop up on the screen.
Now, this is because Cat has read the file from standard input and outputted it to standard output,
which is by default connected to our terminal.
Now here's a mini challenge.
Okay?
I want you to get CAT to read standard input from input text and redirect that output to hello text
and make it so that that happens.
Okay.
So go ahead and give that a go.
Pause the video now and try to get CAT to read standard input from input text and redirect the output
to hello text.
Then come back to see how you did.
Did you manage?
It was, here's how I would go about it.
I would first redirect cat's standard input so that it comes from input text.
So we're just going to do like that.
And then I would output it to a file called Hello Dot Text.
Now to make it easier to read, I might add the stream numbers.
So I might say, okay, I'm going to redirect into stream zero input text and I'm going to redirect
from stream one, which is standard output to hello text.
It's totally up to you which one you use.
This might be a way you do it.
It might not be.
But just remember that zero is standard input, one is standard output and two is standard error.
Okay.
And here we can see that we're reading input text into standard input.
It's quite easy to see that quite, quite graphically.
And we're writing from standard output into hello text.
Okay.
So if we go ahead and do that now, we should be able to see that in fact, yes, we now have a file
called Hello Text and when we open it we see hello world.
Isn't that amazing?
That's how you can redirect data all around your computer.
Now, here's one more mind blowing thing.
Everything in Linux is actually treated the same as a file, even terminals.
So let me open up another terminal.
I'm going to use the shortcut control Orton T and I'm going to open them right next to one another.
So I'm going to put one over here and I'm going to put one over here.
Okay.
Now in the other terminal, this one over this new one on the right, I'm going to run the t y command.
Now, the t command will actually tell us where this terminal is located, where on the file system
it is.
And we see it comes back with slash dev, slash RTS, slash one.
Okay.
Now what I'm going to do is I'm going to redirect the, the, the content from input text.
So just like this and instead of writing it to a file, I'm going to write it to this location, slash
dev, slash RTS, slash one.
So slash dev, slash RTS, slash one.
And when I press enter.
Oh my goodness.
We can see that the data has been passed from one terminal to another.
It's kind of like a really rubbish instant messaging system right now.
Well, this isn't particularly useful in itself.
It shows how redirection can be used to pass data around your entire computer and even over computer
networks.
I've heard.
So that's pretty amazing.
So how awesome you now know everything you need to know about redirection in Linux and the beauty of
the Linux system should begin to be making itself clear.
Now, as a quick recap, you learn that standard input, standard output and standard error are streams
of data and like normal water streams, their flow comes from somewhere and goes to somewhere.
Redirection is all about controlling that, where using redirection you can control where standard output
and standard error streams go and you can control where standard input comes from.
You also learn that these data streams have numbers associated with them.
Standard input is stream number zero.
Standard output is stream number one and standard error is stream number two.
You then learned about the different symbols that can be used for redirection.
They are basically the greater than and less than signs.
Just remember that if you're redirecting output, one greater than sign means that the file will be
erased or truncated before you write to it.
So if you don't want to lose the data that's in there, make sure to use to greater than science to
append data to the file.
In other words, add data to what's already there.
Now there are a few more cool things that you can do with redirection, but they're less common than
what you've learned in these last few videos.
So if you're curious, I've put a link where you can learn more about redirection in the lecture resources.
But one question that must be burning in your head right now is How can I redirect the output from one
command to the input of another?
Surely there must be a way.
Well, that process is known as piping, and it is, in my opinion, that that's the crown jewel of
the Linux command line.
Piping is probably the most important thing that the command line lets you do because it takes all of
the commands that do one thing very, very, very well and allows you to tie them together seamlessly
to do basically anything that you want.
Mastering piping will definitely give you superpowers.
So to master the power of piping and gain super powers.
I'll see you in the next video.