Check out a free preview of the full Introduction to Bash, VIM & Regex course

The "Pipes" Lesson is part of the full, Introduction to Bash, VIM & Regex course featured in this preview video. Here's what you'd learn in this lesson:

James shows the pipe operator, which can feed the output of one program to the input of the next.

Preview
Close

Transcript from the "Pipes" Lesson

[00:00:00]
>> James Halliday: We've seen the kinds of file redirects now. The other kind of redirect that we can use is called a pipe. A pipe is the character on the keyboard, like on a US keyboard if you hold Shift it's the same one as the Backspace character, it's right there.

[00:00:18]
On my keyboard, it's between the Backspace and the Enter key on the far right. But, I don't know, on different keyboards they put it different places. And the pipe character is used to take the output, the standard out of one program, and copy it into the standard input of a second program.

[00:00:39]
This is a really powerful idea in Unix because it means that you can compose programs. So you can kind of make a bigger program, on the fly, that combines the functionality of two simpler programs to do something that you wanna do. For an example of that, if we have, so suppose that we want to know how many files and directories are in the current directory, right?

[00:01:08]
We already know all of the commands that we need to get this output, we just have to combine them with the pipe character. So we already know that the ls -1 command prints out every file in directory, on a single line, right, line by line. So what we can do is take the output of the ls -1 command, and we can pipe that into the wc -l command.

[00:01:36]
And what this is gonna tell us is the number of files and directories because ls -1 prints a new line every time, and we know to count the number of lines. So now we have a program right here that will tell us the number of files and directories.

[00:02:03]
>> James Halliday: If we can modify this program to print, maybe we wanna know how many TXT files there are in the current directory. So we can take that program and modify it to use a wild card, a *.txt. And now the program tells us that there are four TXT files in the current directory.

[00:02:27]
So we can kind of ask these ad hoc questions using existing tools and combining them together.
>> James Halliday: What's really nice about pipes as well is that you can always inspect the data that's going into one program. So if you leave off the pipe you can see, okay, that's the output of that command.

[00:02:51]
So I can read that, I can make sure that it makes sense. And then I can pipe it into the other command and make sure that that makes sense. You can keep going too, so if we wanna see how many bytes of output there are, which would sort of correspond to, I think, the base ten logarithm of the number of files.

[00:03:14]
Then we can pipe that into wc -c to count the number of bytets, which I think ought to be 2. Because there's the number 4 and then there is a new line, so yeah, 2. Or maybe if we get rid of the *.txt, I think there were 11 files, so that would be 3 bytes, right?

[00:03:35]
So one, one, new line. So yeah, 3 bytes, pretty cool. And you can chain together these programs as much as you want to make any kind of new program on the fly.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now