Extend Linux Commands with a Pipe

Piping may draw up all sorts of images in your head that have nothing to do with dedicated servers or Linux, but the use of the pipe on a server is a great way to make your single-line commands more effective.

The pipe is actually the symbol “|”, which is right above the Enter key on most standard keyboards. It shares the key with the backward slash “\”, so you need to hold Shift to use it. In Linux and other Unix-like operating systems, the pipe allows you to forward the output of one command into the next.

For example, if you type the “ls” command, which lists the contents of the current directory, and you want those results to appear one page at a time, you could type a pipe after “ls” and follow it with the “more” command.

ls | more

This is a basic example, but the pipe can be used for much more complex command line strings and can even be used more than once.

As another example, suppose you want to list all html files in a directory and want the results paginated. Just type:

ls | grep html | more

You can combine commands using a pipe with any command you have learned that produces text output. The “cat” command, for example, can output the contents of a file that you can then manipulate.

cat document.html | grep username

You can even work within two separate files.

cat file1 | grep word >> file2

That command will find anything in file1 with “word” in it and write it to the end of file2.

The possibilities with the Linux pipe are vast. Experiment and see what you can find. Just remember that it is a very powerful tool.