One of the most powerful and elegant features of the Linux command line is the pipe, represented by the vertical bar: |.

It looks simple, but this little symbol is the secret weapon for turning a handful of small, simple commands into a single, powerful tool.

What Does the Pipe (|) Do?

In the simplest terms, the pipe takes the output of one command and feeds it directly as the input to a second command.

Think of it like an assembly line:

$$\text{Command 1} \quad \rightarrow \quad \textbf{Output} \quad \rightarrow \quad \text{Pipe } (|) \quad \rightarrow \quad \textbf{Input} \quad \rightarrow \quad \text{Command 2}$$

The beauty of this is that instead of having to save the results of the first command to a temporary file, the data flows instantly from one utility to the next.


3 Practical Examples

Here are a few ways you can use the pipe to make your work on the command line faster and more efficient.

1. Counting Files of a Specific Type

Let’s say you want to quickly count all the .txt files in a directory.

  • Command 1 (ls -l): Lists all files and details.
  • Command 2 (grep ".txt"): Filters the list to only show lines containing .txt.
  • Command 3 (wc -l): Counts the number of lines (which is the count of your files).
ls -l | grep ".txt" | wc -l

Instead of manually counting, you get the exact number instantly!

2. Finding a Running Program

If you know a program is running, but you don’t want to sift through the entire process list, use the pipe to filter the results.

  • Command 1 (ps aux): Lists all running processes on the system.
  • Command 2 (grep "firefox"): Filters that list to only show lines mentioning the string “firefox.”
ps aux | grep "firefox"

This is much quicker than scrolling through hundreds of lines of processes. (A quick tip: you might need to use grep -v grep to exclude the grep command itself from the output, but the basic command above is a great starting point!)

3. Reading Large Files Easily

The cat command displays the entire contents of a file all at once, which is fine for small files, but terrible for large ones. You can pipe the output to a pager like less.

  • Command 1 (cat /var/log/syslog): Tries to print the entire system log.
  • Command 2 (less): Allows you to scroll and search through the output page-by-page.
cat /var/log/syslog | less

Now you can read that massive log file comfortably without it flying past your screen.

Conclusion

The pipe (|) is an essential concept for anyone who uses the Linux command line. It embodies the core Unix philosophy: “Write programs that do one thing and do it well.”

By chaining these single-purpose commands together, you gain limitless flexibility and power, making complex tasks simple. Now go ahead and try piping a few commands together!