Pipes: Connecting Commands
Prerequisites: Redirection: sending output to files
A pipe (|) sends one command's output straight into the next
command's input. Pipes let you build powerful tools from small, simple
programs — the heart of the Unix philosophy.
Real-world analogy: an assembly line. Each station does one job and passes its work to the next.
printf '%s\n' banana apple cherry apple banana apple > fruits.txt
cat fruits.txt | sort
echo "---"
sort fruits.txt | uniq
echo "---"
sort fruits.txt | uniq -c | sort -rn
apple
apple
apple
banana
banana
cherry
---
apple
banana
cherry
---
3 apple
2 banana
1 cherry
Reading the last pipeline left to right:
sort fruits.txt— sort the lines, so identical ones are next to each other.uniq -c— collapse repeated lines, prefixing each with its count.sort -rn— sort numerically (-n), largest first (-r).
The result: a ranking of the most common lines. This "sort, count, sort" pattern works on anything — IP addresses in a log, words in a document, errors by type.
Filtering with grep
grep keeps only the lines that match a pattern:
printf '%s\n' "INFO start" "ERROR disk full" "INFO retry" "ERROR timeout" > app.log
grep "ERROR" app.log
grep -c "ERROR" app.log
grep -v "ERROR" app.log | wc -l
ERROR disk full
ERROR timeout
2
2
grep -ccounts matching lines.grep -vkeeps lines that don't match.
More pipe-friendly tools
echo "hello world" | tr 'a-z' 'A-Z'
echo "one,two,three" | cut -d, -f2
seq 1 5 | awk '{ total += $1 } END { print total }'
HELLO WORLD
two
15
trtranslates characters.cut -d, -f2takes the second comma-separated field.seq 1 5prints 1 to 5;awkadds up the numbers. (awkis a small language for processing columns of text — Level 2 covers it.)
Common mistakes
- "Useless use of cat":
cat file | grep xworks, butgrep x fileis simpler. Usingcatat the start is fine when it makes a pipeline clearer. - Forgetting to
sortbeforeuniq—uniqonly merges adjacent duplicate lines.
Practice
Given a file of words, one per line, print how many different words it contains.
Show solution
printf '%s\n' red blue red green blue red > words.txt
sort words.txt | uniq | wc -l
3