Home > Articles > Operating Systems, Server > Linux/UNIX/Open Source

  • Print
  • + Share This
This chapter is from the book

Building Pipelines

The power of the command line really comes into its own when you start to pass the output of one command so that it goes to the input of the next, combining commands by using pipelines. A pipeline uses the pipe symbol (|) to string together a number of commands to perform a specific task. As an example, if you use the cat command to display the contents of a file to the screen, but the file scrolls past you, create a pipeline and use the less command so you can browse the file:

username@computer:~$ cat foo.txt | less

To see how this works, break the command into parts, each separated by the pipe. The output of the part on the left (cat’ing the file) is fed into the less command on the right, which allows you to browse the file with the arrow keys.

Pipelines can be useful for finding specific information on the system. As an example, if you want to find out how many particular processes are running, you could run a command like this:

username@computer:~$ ps ax | grep getty | wc -l

Here you count how many getty processes are running (getty is the software that runs a console session). The ps ax command on the left lists the processes on the system, and then the grep command searches through the process list and returns only the lines that contain the text “getty.” Finally, these lines are fed into wc, which is a small tool that counts the number of words or lines. The -l option specifies that the number of lines should be counted. Cool, huh?

  • + Share This
  • 🔖 Save To Your Account