Main image of article Intro to Pipes on the Linux Command Line
An interesting and useful capability in Linux is the use of the pipe on the command line. Pipes take the output of a command and use it as input to another command. Stringing commands together makes performing operations and filtering a straightforward task, without the need to write and then read intermediate files. While it's easy to write C programs and scripts to manipulate your text files, taking the output of one command and using it as input to another is both efficient and not too hard to learn. Since pipes are built into the command line, they're available on almost every version of Linux ever offered. The vertical bar symbol denotes a pipe and is the character above the '\' character on American keyboards. I think you'll find lots of uses for the pipe. To see how they work, let's take a look at some examples.

Simple Example

We'll use the ls command for our example. ls, on the command line, is used to display the files in a directory. If you type ls without any arguments in a Linux terminal you'll get a listing of files from the current directory. The image below displays what appeared on my screen when I used ls, showing two columns of file names.

Output of ls Command

One problem with this listing is that there are 12 rows of files above the ones displayed that aren't seen. They scrolled by too fast. We could just grab the vertical scroll button on the right side of the window and drag it up to see those missing rows, which is all well and good in an X-Windows environment. But what if you’re logged into a machine remotely using SSH and are only able to interact with that machine via the command line? Pipes easily solve the problem because the output of the ls command can be unceremoniously sent to another little Linux program, called “less,” that displays a screen full of text at a time. rob%  ls | less The next screenshot shows how the files are displayed in a single column. They can be scrolled forward using the keyboard, with the space bar. Scrolling backward (or back up through the listing) is accomplished by typing the B character. Q exits the vi-like environment.

Output of ls command piped to less

Something More Complex

A more sophisticated example might be to use the ls command with awk to show just certain columns of information. I previously discussed awk here. Take a look at the following command line. rob%  ls -l | awk '{print $5, "\t"$6, "\t"$8, "\t"$9}' | less Here, ls -l outputs an expanded listing of files (using the -l option). The image below shows what the listing would look like if executed by itself.

Output of ls -l Command

That output is then piped over to awk, which takes the various selected fields in the file listing and neatly spits them out in tabbed columns. Finally, another pipe sends that output through less, so only one screen is displayed at a time. The image below shows the first screen of the final output. Pretty and easy-to-read, isn't it?

Output of ls awk less

Going Further

You can use all kinds of commands with pipes. Common everyday commands include sort (sorts the output), wc (performs a word count on the output), and grep (pattern matching). has a short cheat sheet on commands that can be used with pipes. Pipes are also frequently used with file redirection, using the >, <, >> and >> symbols, where the final output is sent to a file, which you might later edit or print.