This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In


    Chain Commands with Pipes and Redirect Output in Bash

    Cameron NokesCameron Nokes

    Pipes (|) are one of the most powerful features in bash and allow you to efficiently chain commands together and send data through it. We'll also look at how output redirection works using the stdout redirect operator (>). This allows us to take the output of a command and send it to a file. You can also append to a file using >>.

    Read this article for more information on bash redirection.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    Instructor: 00:00 Pipes can be used to direct the output of one command to the input of another. Let's see what we mean by that. For example, if I wanted to check all of the running processes on my computer, I could use the PSAX command, and that would show that to me.

    00:13 There's a ton of information here. We can see, here's an instance of Chrome running. If I wanted to see all Chrome processes that are running currently, it'd nice if I could grep in this screen. Let's see how we would do that.

    00:28 We do PSAX, and then we pipe to the grep command. Normally, we grep in a file, but when we pipe it, we're basically saying take the output of this command, and pass it as the input to this next command. If we run that, it's only showing us Chrome processes that are running.

    00:48 We could take it even further, actually. We could pipe it again to less, if we wanted to view it in a different way. Another good example of pipes would be if I wanted to see the size of a file after it's been uglified and GZIPed.

    01:03 I've already installed the uglify JS command globally via NPM. I'm going to call that, and I'm going to pass the compress and mangle flags. Then we do two dashes, and then the file name that we want to compress.

    01:18 From there, I'm going to pipe that to GZIP. I'm going to the pass the nine flag, which tells it to GZIP on the maximum setting. From there, we need to know the file size of these two commands. From there, I'm going to pipe it to the WC command.

    01:31 WC stands for word count. I will pass it the C flag, which tells it to give me not the word count, but the bye count of its input. Let's run that, and see what we get. We get 280 bytes, which will be the resulting file size after we uglify and GZIP the index.js file.

    01:48 That's pretty cool. Note that if we LS here, that it didn't output another file for that. This all happened in memory, and it outputted the file size to us. Now, let's see how redirection works. When we run a command like LS, it outputs to the screen, which is also called standard out.

    02:07 If we wanted to redirect that, we could use the angle bracket operator, and give it a file name. We'll call it ls.txt. If we run that, basically what happened is that instead of outputting to the screen, it captured the output, and directed it into a file.

    02:22 This file didn't exist, so it created it on the fly for us. If we output that now, we can see that it outputs the index.js, which is from our LS before. It created the file before it redirected into it. That's why we have the ls.txt there.

    02:35 We can also append to a file. Just using the echo command, I can echo "hi." Instead of outputting that to the screen, I can direct that and append to the ls.txt file. Then count it out. We can see we have hi there at the end now.

    02:53 There are a lot of possibilities with input and output redirection and piping. See Bash's documentation for more details.