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.
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:
cd ~/project
cat logs/app.log | wc -l8cat 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:
cat logs/app.log | grep ERROR2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeoutgrep ERROR keeps only the lines that contain that word. Add a counting stage:
cat logs/app.log | grep ERROR | wc -l2You 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:
sort data/fruit.txt | uniq -c | sort -nr 3 apple
2 banana
1 date
1 cherryMemorise 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:
grep ERROR logs/app.log | tee errors.txt | wc -l
cat errors.txt2
2026-01-15 10:02:40 ERROR database timeout
2026-01-15 10:02:44 ERROR database timeoutUse 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:
ls missing-folder | wc -lls: cannot access 'missing-folder': No such file or directory
0The 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 |&:
ls missing-folder 2>&1 | wc -l1Exit 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.
Ready for the next small win?
Mark the lesson complete when you are ready.