Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pipes Command Guide: Chaining Commands Together

The pipe (|) connects the stdout of one command directly to the stdin of another, letting small single-purpose tools combine into powerful workflows — the defining idea of the Linux command line.


What is a Pipe?

A pipe is a direct wire between two commands. Instead of one command writing to your terminal and you manually feeding that output into another, the pipe streams the first command’s stdout straight into the second command’s stdin:

command1 | command2

Data flows left to right, continuously, as it’s produced. The classic example:

cat access.log | grep "404" | wc -l

cat reads the file, grep filters the lines containing 404, and wc -l counts what survives — all without a single temporary file.

Why Learn Pipes?

  • They’re the whole point of Unix. Small tools that each do one thing well become an ecosystem when they can be chained.
  • No temporary files. Filtering, counting, and sorting happen in memory, in real time.
  • They compose. Once you can pipe, every command you’ve learned becomes a building block for the next one.
  • They’re everywhere in practice. Log analysis, text processing, and debugging all rely on pipelines as their basic unit.

A command does one thing; a pipeline does a workflow. Learn to think in chains.


1. Your First Pipeline

Start with something you can verify by eye:

ls -la /etc | head -5

ls -la produces a long listing; head -5 keeps only the first five lines. The listing never touches disk — head receives it as a stream.

The left side feeds the right side. The pipe reads whatever the left command writes to stdout and hands it over as input.


2. Filtering with grep

grep (see its full guide) is the most-piped command in existence — it keeps only the lines matching a pattern:

cat /etc/passwd | grep bash

Show every login shell that ends in bash. Add -i for case-insensitive matching:

ls /usr/bin | grep -i firefox

A common refinement: use grep -v to show everything except the match:

ps aux | grep -v grep

3. Counting and Summarizing

Two commands turn streams into numbers:

cat access.log | grep " 404 " | wc -l        # how many 404s?
cat /etc/passwd | wc -l                      # how many users?
ls *.md | wc -l                              # how many markdown files?

sort | uniq -c is the classic “top N” pipeline — count occurrences of each value:

cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10

Read left to right: take the first field of each line (the IP), sort so identical IPs are adjacent, count each run, sort by count descending, keep the top ten.


4. Sorting, Cutting, and Reshaping

Pipelines don’t just filter — they transform:

cat data.txt | cut -d: -f1 | sort | uniq        # unique names from a colon-separated file
cat data.txt | awk '{print $2, $1}' | column -t  # swap columns and align them

cut slices fields out of each line; awk (covered in a dedicated guide) can print, compute, and reformat; column -t aligns output into readable columns.


5. Redirecting the End of a Pipeline

Whatever a pipeline finally produces can be saved with redirection (see Redirection):

grep -rn "TODO" src/ > todo-list.txt 2> todo-errors.txt

Only the final command’s output is redirected. Each intermediate stage flows through the pipe untouched.

You can also split a pipeline’s output with tee when you want to watch and save at the same time:

long-task | tee progress.log | grep -i error

6. Multi-Stage Examples You’ll Actually Use

A pipeline is only as good as the problem it solves. Some real patterns:

Find the largest files under a directory:

du -ah . | sort -rh | head -10

See which processes are using the most memory:

ps aux | sort -k4 -rn | head -5

Check if a service is running:

ps aux | grep -v grep | grep nginx

Find all unique commands from your shell history:

history | awk '{print $2}' | sort | uniq -c | sort -rn | head -20

Each of these is three or four single-purpose commands fused into one line. That fusion is the pipe.


Common Pitfalls

  • Piping a command that writes to stderr. Errors bypass pipes entirely — cmd1 | cmd2 never sees cmd1’s error messages. Redirect stderr separately (2>) when you need them.
  • Expecting the pipe to preserve everything. Some commands behave differently when their output isn’t a terminal (colors disappear, progress bars vanish, ls switches to one-column mode). Use --color=always or check the command’s man page if output looks stripped.
  • Forgetting that the left side must produce plain text. Binary output piped into grep or sort can produce garbage or “binary file matches” messages. Use strings or cat -v to sanitize first.
  • Over-stacking pipes until nobody can read them. Five stages is often the limit of sanity. If a pipeline needs a comment, write it as a small script with named variables instead.
  • Assuming order doesn’t matter. sort | uniq and uniq | sort are not equivalent — uniq only collapses adjacent duplicates, so sorting must come first.

PageWhy You’d Read It Next
stdin, stdout, and stderrThe three streams pipes connect.
RedirectionSaving a pipeline’s final output to a file.
grepThe filter command that appears in more pipelines than any other.
head / tailCapping a pipeline’s output at the start or end.

Pipes Cheatsheet

TaskCommand
Basic pipelinecommand1 | command2
Filter lines by patterncommand | grep pattern
Count linescommand | wc -l
Top N by frequencycommand | sort | uniq -c | sort -rn | head -N
Top N by sizedu -ah . | sort -rh | head -N
Exclude matchescommand | grep -v pattern
Save pipeline outputcommand1 | command2 > result.txt
Watch and savecommand | tee log.txt | next-stage
Silence everythingcommand > /dev/null 2>&1