How to Use Pipes on Linux

Share
  • September 5, 2019

Fatmawati Achmad Zaenuri/Shutterstock.com

Use Linux pipes to choreograph how command-line utilities collaborate. Simplify complex processes and boost your productivity by harnessing a collection of standalone commands and turning them into a single-minded team. We show you how.

Pipes Are Everywhere

Pipes are one of the most useful command-line features that Linux and Unix-like operating systems have. Pipes are used in countless ways. Look at any Linux command line article—on any web site, not just ours—and you’ll see that pipes make an appearance more often than not. I reviewed some of How-To Geek’s Linux articles, and pipes are used in all of them, one way or another.

Linux pipes allow you to perform actions that are not supported out-of-the-box by the shell. But because the Linux design philosophy is to have many small utilities that perform their dedicated function very well, and without needless functionality—the “do one thing and do it well” mantra—you can plumb strings of commands together with pipes so that the output of one command becomes the input of another. Each command you pipe in brings its unique talent to the team, and soon you find you’ve assembled a winning squad.

A Simple Example

Suppose we have a directory full of many different types of file. We want to know how many files of a certain type are in that directory. There are other ways to do this, but the object of this exercise is to introduce pipes, so we’re going to do it with pipes.

We can get a listing of the files easily using ls:

ls

Collection if files in a directory, in a terminal window

To separate out the file type of interest, we’ll use grep. We want to find files that have the word “page” in their filename or file extension.

We will use the shell special character “|” to pipe the output from ls into grep.

ls | grep "page"

ls -l | grep "page" in a terminal window

Read the remaining 61 paragraphs

Source : How to Use Pipes on Linux