Q1What does the pipe | symbol do?
Pipes — Chaining Commands
Learn how a pipe passes one command's output to the next, counting lines with wc -l, sorting with sort, and the classic sort | uniq to collapse duplicates — hands-on in a browser terminal.
What Is a Pipe — the | Symbol
The pipe | is the symbol that passes one command's output straight to the next command as its input.
By processing one command's results with another, you combine small commands to get the job done.
This is the Unix design philosophy of building up processing by connecting single-purpose tools.
| passes the left output to the right input, chaining single-purpose commands.echo apple > fruits.txt # create line 1
echo banana >> fruits.txt # append
echo cherry >> fruits.txt # append
cat fruits.txt | wc -l # count lines -> 3
ls / | wc -l # number of items in root
Anything Can Be Connected
Pipes aren't tied to specific commands.
Any command that produces standard output and any command that accepts standard input can be freely connected.
There are countless combinations: ls | wc -l for a file count, cat file | sort for sorting, and so on.
echo log1 > lines.txt # create line 1
echo log2 >> lines.txt # append
echo log3 >> lines.txt # append
cat lines.txt | wc -l # 3
ls /etc | wc -l # number of items in /etc
| Command | Role in a pipe |
|---|---|
| | Passes the left output to the right input |
wc -l | Counts the lines it receives |
sort | Sorts the lines it receives alphabetically |
uniq | Collapses adjacent duplicate lines into one |
head / tail | Takes just the start / end of what it receives |
Sorting — sort
sort is the command that sorts the lines it receives.
By default it sorts alphabetically (in string order).
It can take the previous command's output through a pipe and pass the sorted result on to the screen or the next command.
sort sorts the lines received through a pipe alphabetically and outputs them.echo banana > fruits.txt # create out of order
echo apple >> fruits.txt
echo cherry >> fruits.txt
cat fruits.txt | sort # sorted to apple / banana / cherry
Collapsing Duplicates — uniq
uniq is the command that collapses consecutive duplicate lines into one.
uniq looks only at adjacent lines, so it can't collapse identical lines that are far apart.
When you want to reliably reduce duplicates to one line, the classic approach is sort | uniq: gather the same lines next to each other with sort first, then connect to uniq.
sort gathers duplicates side by side, and uniq collapses consecutive duplicates into one.echo pear > list.txt # create with duplicates
echo apple >> list.txt
echo pear >> list.txt
cat list.txt | sort # apple / pear / pear
cat list.txt | sort | uniq # apple / pear
sort before uniq
uniq treats only adjacent lines as duplicates.
It can't collapse identical lines that are far apart, so when you want to reduce duplicates to one line, sort first with sort and then connect to uniq.
Knowledge Check
Answer each question one by one.
Q2What is shown when you run cat fruits.txt | wc -l?
Q3When you want to collapse duplicate lines into one, which command is effective to connect before uniq?