So here we are, you beautiful people.
You're about to learn the crown jewel of the Linux operating system piping.
So in the last few videos you've learned that you can use redirection to change the location of where
you send command output and where you get command input.
But what if you wanted to connect the standard output of one command so that it flowed into the standard
input of another command?
Well, that's where piping comes in and it's ridiculously powerful.
Each Linux command is designed to do one task extremely well.
So if you can continually pipe these highly specialized commands together and pass data between them,
you can build advanced pipelines to do pretty much any task that you can think of.
And once you know how to do it, it will really feel like a super power.
So in this video, you're going to learn how to build your very own command pipelines.
And then in the next video in part two, you're going to learn how to make your pipelines more flexible
by using the T and the X args commands.
This is going to be a great section of the course and you will not recognize yourself after you learn
what I'm about to teach you.
So I hope you're excited.
Let's go ahead and get into it.
Okay.
So piping is all about taking the standard output of one command and connecting it to the standard input
of another command.
So let's take, for example, the date command.
The date command, as we know, will show us the date.
Now we can write the standard output of the date command to a text file called Date Dot text using redirection
so as a mini challenge.
See, see if you can remember how to do that.
How would you take the date command and redirect it into a file called date txt.
Give it a go and then come back to see how you did.
Did you manage it?
Well, this is one way you could do it.
You could type the date command and then say, I want to redirect the standard output into date txt.
Now you could have just had the symbol like this, or you could have put the number one to indicate
that you wanted the the first data stream, which is standard output to be written to data.
And if you did that massive congratulations, you're learning already.
But now that we've done it, we are going to take a look at our file explorer here and we see a file
called Data txt and we see that the data information has indeed been written into that file.
But now what if we wanted to just cut out the day of the week, which in this case is Monday?
How would we cut out just the day of the week?
Well, one way to do it would be to use the cut command.
Now, you haven't seen the command before, but it works very simply, and it's probably easier to show
you by doing.
First we want to read the date txt file into the standard input for the cut command.
So to do that we would type cut and then read into the standard input date.
TXT okay.
And you don't have to have the zero, so you could just get rid of the zero and just just do that,
have the lesson sign and then data and remember this space here, this is optional.
Okay, I'll probably just keep it because it looks a bit nicer.
Now what will do what the command does is it will cut up a file and give you specific columns.
So if we look at data txt, you can imagine that the file is broken up into columns with a space between
each column the day of the week.
In this case, Monday is the first column or the first field, as it's known in the command.
This would be the second field, this would be the third field and so on and so on.
But we want the first field now to make the command work correctly for us.
We need to give it two pieces of information using options.
First, we need to give it the delimiter long form option.
So delimiter and what what a delimiter is, is it tells you what divides the columns.
So we need to tell it that spaces are what divide or delimit the columns because spaces are what divide
up these specific columns in the file.
So to do that, we just type delimiter as a long form, put a space and then in quotes put another space.
So we're saying a space is what limits this file or this input.
Then we need to say which field we want now that we've told it, how the file is cut up.
So we give it again the long form option, which is fields and then a space and then one.
And now what we've got now when we run that, what we're going to see is we're saying to the command,
okay, read data txt it's broken up into columns with us, each with a space separating each column
and we want the first field.
Now when I press enter, you can see that we get Monday as an output.
Isn't that awesome?
Right.
That's pretty cool.
You've not seen the command before, but it's a very cool command.
But this this works, but it's kind of clunky.
Okay.
First, we're writing the standard output of the date command to a file which takes up space on our
computer.
Right.
Then we have to read that file into the command.
And maybe we would also, after getting this data out, we then have to delete this original file because
we don't need it anymore, which is another process that we'd have to undertake.
So doing it this way by saving it the data into a file and then reading that file in two steps takes
a lot more typing.
It makes unnecessary files, which then you've got to remove, or they just take up space and it's just
an inefficient and awkward way of doing things.
So instead we can pipe the standard output of the data command directly into the standard input of the
command.
Okay.
So if I just use the up arrow key to keep what we had before, now instead of saying read into the read
into the standard input of the command data.
XRT What I'm going to do is I'm going to type date and then use this pipe symbol.
Now this pipe symbol you can find by holding shift and then backslash on your, on your most keyboards
anyway.
So yeah, that's the way it is on most keyboards, but if it's not there on your keyboard, if it's
not possible by holding shift and backslash, just hop onto a search engine and look for how to do the
pipe symbol on your keyboard layout.
If you have trouble finding it, you should come up straight away.
But now what's happening is we're saying pipe the data from the date command standard output into the
cut commands standard input.
Now because data FD was effectively the standard output from the date command, this is going to have
the same effect.
So when we press enter Delta, we get Monday out and this happened all in one step.
So that's really cool.
We managed to take the data straight from the date command and pipe it into the standard input of the
cut command so that it could be operated on.
We gave the command some options that allowed us to cut out directly the name of the day of the week.
Now I'm just going to press the up arrow key, and now I'm going to show you something really cool.
What we can do is we can actually output that data into a.
File.
Okay.
So we could actually save that using redirection into a file called today.
TXT.
So check this out.
We're using the date command to get the all this information here.
We're cutting it out to get just Monday and then we're writing that data into a file called today txt.
Pretty awesome.
Right?
So let's try it.
Let's go ahead and press enter delta.
And now that's gone out.
It hasn't come up on the screen this time.
And if we look in here, we've got a file called today txt and if we click it, we've now got Monday.
Isn't that awesome?
I think that's fantastic.
Now, as a side note, it doesn't actually matter where I put the redirection, so I can have the redirection
at the end here.
I could have the redirection over here and it would still work exactly the same.
Okay, that works absolutely fine.
I could even actually put it in between the different options so I could put it here.
And I will also work exactly the same.
We're still getting today's text with Monday inside.
So that's just something to bear in mind.
Okay.
Don't be scared about typing stuff wrong.
All of these are valid ways to do it, but some are just a lot more readable than others.
But let's say that instead of redirecting, we wanted to send the standard output of the cut command
to yet another command.
How would we do that?
How would, instead of saving it into a file, how would we send the data from the Cook Command into
yet another command?
Well, it's simple.
You just add another pipe.
So you just say, okay, let me cut that out.
Sorry.
Let me just put another pipe and then let's say I'll put a command and I've got some options and then
I've got some command line arguments and that's how that would work.
This new command could take its own options, its own command line arguments, and it could also read
standard input from the command.
So you can see how this works.
We're taking the date command piping that output into standard input of the command, giving the command
its own options.
Then we're piping that output into another command, giving that command its own options and its own
command line arguments and so on and so on and so on.
You can just keep on going, piping these things together, doing what you want, and giving them options
as you wish.
So this is what gives the the whole Linux terminal its power.
Up until now, we've been doing things that are cool but not really necessarily useful.
But when you can combine together really powerful commands and have them operating exactly way exactly
the way that you want to and pipe the data between them and all over your computer, it really gets
amazing.
So you can see that this piping stuff can go on and on and you can see how to build up powerful pipelines
this way.
It's actually really straightforward.
You just pipe together different commands.
Isn't that awesome?
So let me ask you a question.
What if we wanted to actually keep the whole date in a file and then pass it into a pipe?
Well, we can't simply use a redirection at the end here because we're already passing the the standard
output of the date command into the command.
Right.
So we can't really save it in a file because we're already passing it on down the pipeline.
And if we tried to do it, if we tried to just get rid of the NDA, if we tried to just pipe the output
into TXT.
If we do that, then let me just delete everything we've got in here so we can see the effect.
If I try this, we'll see that all we get is just data.
TXT The whole data is saved into data, but then there's nothing to pass on into the command.
This is because redirections are processed before pipes when in the shell.
So it will find the redirection and do that before it ever sees the pipe.
And this basically breaks our pipeline.
So the data can't really go two places at once.
Or can it?
Well, to make this work, we're going to need to use a command called the PT Command.
And it's these kind of advanced stuff that we're going to be showing you in the next video.