Skip to lesson

Linux Command Line · Lesson 15 of 43

Pipes: connecting commands together

Connect commands with the pipe operator so the output of one becomes the input of the next, and build useful one-line data pipelines.

Beginner to intermediate·12 min·Text, redirection and pipes·Updated 2026-11-24

UNDERSTAND IT. THEN TRY IT.

What you can do after this lesson

You will combine small commands into pipelines that answer real questions: how many errors are in a log, which word is most common, which files are biggest. This is the idea that makes the command line more than a collection of separate programs.

The pipe operator

A pipe, written |, connects two commands. The standard output of the left command becomes the standard input of the right one, with no temporary file in between:

bash
cd ~/project
cat logs/app.log | wc -l
result
8

cat printed the log, and wc -l counted the lines it received. Read pipelines from left to right as a series of steps, each one transforming the text.

Build a pipeline step by step

Real pipelines grow one stage at a time, and checking each step is the best way to get them right. Suppose you want the number of errors:

bash
cat logs/app.log | grep ERROR
result
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeout

grep ERROR keeps only the lines that contain that word. Add a counting stage:

bash
cat logs/app.log | grep ERROR | wc -l
result
2

You can limit long output with head, which stops reading when it has enough lines. For example, ls -l /usr/bin | head -n 3 shows only the first three lines of a very long listing, and the exact lines differ between systems.

Ranking things: sort, uniq and a classic pattern

One of the most useful patterns counts how often each distinct line occurs, then ranks them. uniq -c counts adjacent duplicates, so the input must be sorted first, and sort -nr orders numbers from largest to smallest:

bash
sort data/fruit.txt | uniq -c | sort -nr
result
      3 apple
      2 banana
      1 date
      1 cherry

Memorise this shape: sort | uniq -c | sort -nr. It answers "what is most common here?" for fruit, web addresses, error codes or anything with one item per line. The next lessons introduce sort and uniq in detail.

tee: save a copy and keep going

Sometimes you want a file with an intermediate result while the pipeline continues. tee writes its input to a file and also passes it on:

bash
grep ERROR logs/app.log | tee errors.txt | wc -l
cat errors.txt
result
2
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeout

Use tee -a to append instead of overwriting. tee is also how you write a file with sudo when a plain > would fail, as in echo text | sudo tee /etc/example.

Errors and pipes

Only standard output goes through a pipe. If the first command writes an error, it still appears on the screen and not in the next stage:

bash
ls missing-folder | wc -l
result
ls: cannot access 'missing-folder': No such file or directory
0

The count is 0 because nothing travelled through the pipe, and the error message went to the screen. To send errors down the pipe as well, use 2>&1 | or the Bash shorthand |&:

bash
ls missing-folder 2>&1 | wc -l
result
1

Exit status of a pipeline

A pipeline's exit status is, by default, that of the last command, so a failure earlier in the chain can go unnoticed. In scripts, run set -o pipefail so that the pipeline reports failure if any stage fails. The scripting lessons return to this.

Why this works

Each stage is a separate small program that reads lines and writes lines. They run at the same time, and the system hands text from one to the next. That is the Unix design from the first lesson: no program needs to know about the others, and you glue them together as the problem requires.

Common mistakes

Using cat when a command can read the file. grep ERROR logs/app.log is shorter than cat logs/app.log | grep ERROR. It is fine while learning, but you can drop the cat when the next command accepts a file.

Forgetting to sort before uniq. uniq only merges adjacent lines.

Expecting errors in the next stage. Only standard output is piped.

Piping into a command that does not read input. ls | rm does nothing useful: rm takes names as arguments, not from input. The xargs lesson shows how to bridge that gap.

Exercise

Count the lines containing INFO in logs/app.log. Then show the three most common words in docs/notes.txt by splitting it into one word per line with tr ' ' '\n' and using sort | uniq -c | sort -nr | head -n 3. Save the intermediate sorted word list with tee words.txt and check it.

KNOWLEDGE CHECK

A quick check. You’ve got this.

Try each question. Every answer comes with an explanation.

0 / 4 understood
01 What does cat logs/app.log | wc -l do?
02 What does tee do?
03 Why is sort needed before uniq -c in sort | uniq -c?
04 Do error messages travel through a pipe?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses