Pipes and Filters

When we type a command or run a program we will get an output printed to the terminal. Pipes can redirect a command or a program’s output to a file for future reference or to be used at a later time. In this section you will learn how to use the pipes and how to combine pipes into powerful single-purpose programs.

The first command to learn is >. This pipe, used as command > file redirects a command’s output to a file. For example:

$ ls  
theme.mp3 boss.mp3
$ ls > output.txt
$ cat output.txt
theme.mp3
boss.mp3

Sometimes you run into the problem of redirection mutliple outputs to a single file. If you use the > command the original file contents will be overwritten. To append new outputs to a file you can use the command >>. For example:

$ cat output.txt
theme.mp3
boss.mp3
$ echo Hello! >> output.txt
$ cat output.txt
theme.mp3
boss.mp3
Hello!

If you would like to read the contents of a file to use in your command you can use the < command. In this case instead of redirecting an output to a file you are redirecting a file’s contents into the input of command. For example:

$ rm *.mp3 < output.txt 
$ cat output.txt
Hello!

Sometimes in order to combine two command line steps you can use a pipeline. That is you can use the output of one file into the input of another file using the | command. For example:

$ ls
dir5 dir37 dir40 file1.txt 
file2.txt music papers
$ ls | head -3
dir5 dir37 dir40

Do Exercise #3


Contents ⬅ Previous Pipes and Filters Next ➡