Redirecting input and output of a program

In a unix terminal, in contrast to the windows command prompt, you are able to redirect all output of any program, e.g. to a textfile. This is done using the so called piping feature of the shell. There are three special characters that tell your terminal to pipe data from a program into a text file (>), to pipe a text file as input into a program (<) or to pipe the data printed by one program as input into another program (|). E.g. the call
$ ls -l > dirlisting.txt
will redirect the output of the ls program into a textfile named dirlisting.txt. Note that using this command any previous content of the textfile dirlisting.txt will be overwritten if this file already exists! Otherwise the file will be created. To append the output of a program to an already existing file you can use the special piping symbol >>

$ ls -l >> dirlisting.txt

These features are very powerful if you want to use several commands to process your data where the data output printed in each intermediate step is only used as input for the next steps and not needed after processing all commands. E.g.

$ ps -e | grep -i terminal | wc -l
will first use the output of the cat program for the file /proc/cpuinfo as input for the grep program, which in turn searches for lines containing the string "terminal", ignoring the characters case (case insensitive, -i). As we already know, the grep command prints out the lines where it finds the search phrase. This output will be used as input for the wc program which then counts the number of lines in its input. This is useful for example if you want to know how many times the terminal program runs on your system.

Another way to do the same would be calling each program seperately and piping its output into a new textfile. Then, using the textfile produced by each previous step as input, the next command is invoked and again, the output has to be written into a new textfile.

$ ps -e > textfile1
$ grep -i < textfile1 > textfile2
$ wc -l < textfile2

The problem doing it this way is that we now have two textfiles that we have to delete as we do not need the residing data anymore. Using the example with the | piping above we circumvent this problem by not generating intermediate textfiles in the first place. So if you get more involved with the terminal it might be very useful to use piping.

Ronny Lorenz 2010-04-06