When you type a command in you'll typically see some kind of output.
But what if you wanted to do something with that output?
Maybe you want to save the listing of a directory to a file.
This is exactly what redirection and piping allow us to do.
Redirection allows us to redirect input and output of commands to,
and from files as well as connect multiple commands together into
pipelines.
Piping allows us to send the output of one command as the input of another.
Now you can start to picture how we can use both of these to create pipelines.
And it's actually quite creative because this means that there are multiple ways
to get to a very similar result. Let me show you.
Let's start off with redirection.
So first I'm going to just list a directory that I have. Uh,
it's going to be the user bin directory,
and you can see there's a bunch of details in here.
What if I wanted to save this to a file? Well,
this is something we can do with redirection. Let's type in that same command
LS stands for list. The dash L stands for long.
So it's a listing with more details,
and then we're going to provide that same directory.
And this time we're going to use something called the redirection operator,
which is the greater than symbol. Finally,
we're going to provide a file name for our output to be saved to.
I'm gonna go ahead and hit enter. Now, this is a new file. So it gets created,
but say you already had this file and you run this command since we're using the
redirection operator, it's going to get overwritten. So if I do this cat,
which will just output the contents of the file to the screen,
we can see it's the same as what we got before.
When we actually listed to the screen. We do have the option to append.
So say we do something like, hello, echo, hello.
And then in this time we'll use the append redirection operator and we'll
provide that same file name.
Now I print out the contents and we can see it's the same,
except that the end the hello was added. Alrighty,
let's take a look at piping.
I'm going to clear the screen using the pipe operator,
which is this one right here.
The output of one command can be piped into the input of another
file. Let's use the list command with the head command.
So what we can do is type in LS dash l.
Have that same user bin directory that we've been using. Pipe operator,
head. There we go. Might be able to make this a little bigger.
So it's easier to see here. All right.
So you can tell that the first time that we ran this command,
where we listed straight to the screen, we got a lot more of output.
The output in this case is 10 lines and the head command
lists a certain amount of lines of output.
And the reason there's only 10 is because head when you don't provide how many
lines you want, I'll put it to the screen. It's going to default to 10.
If I were to do head dash five,
you can see we only get five.
Now we can combine redirection and piping and in order to better
filter and really customize the output that we want. So let's clear the screen.
Now let's do something like
LS dot O that same directory,
and then say,
I want just five lines.
The first five lines here,
we're going to create a new file called sample1.TXT.
Fantastic. So now I print that out to see the screen and you can see,
we only have five lines here because what happened is the list command
sent its output to head.
Then head grabs the top five lines of that output and sends it to
sample. Now we can also use the append here.
So what if we did something like LS -l ,
by the, by now you should be very familiar with this LS command.
And this time we're going to use something called tail,
which is very similar to head except instead of grabbing the top end number
of lines, it's going to grab the bottom end number of lines.
So I'm going to say tail dash five, again,
it uses 10 by default, I'm going to get five. And then instead of overriding,
I'm just going to append to sample. Now,
if we print that out, we can see there's more in here.
And then this is the bottom five lines.
We'll clear this and that is it for this demo.
That's it for this demo, we've reviewed piping and redirection.
We know how to redirect the output of one command to a file.
We explored how to pipe the output of one command to another,
and even save that output to a file.
We also briefly touched on the head and tail commands and we realized how
creative and beautiful a command line can actually be. Alrighty Cloud Gurus,
that's it for this episode of ACG Fundamentals,
go on and get creative with some of these commands and let me know how it went
in the forums. Keep Being Awesome, and I will see you in the next one.