Terminal 101: Piping Output
Posted 05/07/2012 at 10:44am
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Over the past few weeks, we’ve covered more than a few Terminal commands that generate a lot of on-screen text output, but as the text is "printed" to the screen, it can be difficult to read in a Terminal window, especially when multiple lines of text are involved. We’ll show you how to get around that problem by "piping" the output of Terminal commands into a text file that can be stored on your Desktop and opened in a text editor to read at a later time.
There are two ways that you can output text to a file from Terminal (a.k.a “piping”). We’ll show you both ways.
1. Piping Data Without On-screen Output
One of the easiest ways to redirect the output of a Terminal program to a file is by using the “pipe” command. This command uses one or two "greater than" signs, depending on if you want the file to be overwritten or appended to.

Using the single redirect command, the output file will be overwritten each time the command is run. We’ll try redirecting the output of the ifconfig command so that we can have a copy of our network information for later reference. We’ll use the following command:
ifconfig > network.txt
Notice that no output was printed to the screen, rather, the information was redirected from the standard output (the screen) to the file named network.txt.

To append to the file that was created, use the double redirect sign (>>) instead of the single redirect. The original file will have the newer redirect data added to the bottom of the file.
2. Piping Data With On-screen Output
If you wish to see the data on the screen, but also want a text record of the output (a log file), then you’ll want to use the tee command in conjunction with the command that you wish to capture the input from. This command will do the same thing as the other redirect method above, except also print the output to the screen for instant viewing.

We’ll use the same command as above:
ifconfig | tee ~/network.txt
Notice what we did there. We type the command that we wanted to capture the output from, followed by a space, a “|” (pipe symbol on keyboard, located above the enter key), another space, and the tee command followed by the location for the output text file.
When this full string of command is run, you will see the output from the ifconfig command, and a text file will be created with the identical output.
There are literally tons of uses for the pipe command. Let us know how you’ll use it in the comments below.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.