In this video, you'll learn about I-O redirection
and piping.
So what is it all about?
Well, to start with, there's redirection.
In redirection, you use the standard in, the standard error,
and the standard out to work with command input and output
in a flexible way.
What is standard in?
That's the standard input, normally your keyboard.
Standard error is where commands are normally
sending error messages to, and standard output
is where commands are normally sending the output to.
And the characters to use is greater than.
Greater than is for redirecting your output to a file.
If the file that you are redirecting to already exists,
well, it will be overwritten with a new file.
And if you don't like that, use a double greater than
in append mode, so that will add to the end of an already
existing file.
Two greater than will send the standard error to a file,
or in this case, to the dev null device,
because sometimes you just don't care about error messages.
You want a clean result in the command.
So two greater than dev null will do that for you.
Not so very commonly used is a smaller than sign.
And a smaller than sign is redirecting the standard input.
But most commands don't need the smaller than sign,
because they allow you to specify a file name
to be used as standard input.
Another common feature in Bash is piping.
So in piping, the standard out of the first command
is used as standard in of the second command.
As in PSAOX, pipe grep SSH.
Let me show you.
So when I use LS, then this is what I get.
And when I use LS on a non-existing file,
then this is what we get.
First example is an example where the standard output
is shown on screen.
And the second example is an example
where the standard error is shown on screen.
So if I use LS greater than outfile,
we don't see any output, because the output is written to outfile.
To check the contents of outfile, use cat on outfile.
And there we can see that the output
has been written to this file.
Next, if I use who greater than outfile,
then what is happening?
Well, outfile is overwritten, this time
with the output of the who command.
And that's why you might be interested in using
a double redirect.
So I'm using LS double redirect outfile.
And now if we cat outfile, there we
can see that the output of LS has been appended.
Now, how about these errors?
Well, LS non-existing file to greater than errors
is writing a file with the name errors.
And that can be convenient if your command is generating
lots of errors and you do want to understand
what is going on.
So you have your errors in a file
that facilitates further analyzing of what's going on.
Another common way of using errors
is by sending them to the null device.
Dev null is the discard device.
Anything you send to the dev null device
is to be never seen again.
And you might want to do that if a command is producing
lots of errors that really you don't care about.
Let me also give you another example of piping.
We have already seen piping when we
were talking about man minus k.
But this time, another example.
PSAOX is listing all processes that are currently
active on this system.
Now, it's unlikely that you want to browse
through all the different processes.
Most people, when they use PSAOX,
they are looking for something in particular.
And then grep is a convenient utility.
So the pipe makes that the output of PSAOX
is used as input for grep.
And there we can see the grep command showing
SSHD as currently running.
Piping and output redirection are extremely useful features
of the bash shell.
But there's more, as you will discover
in the next couple of videos.