Pipes are the secret sauce that makes Unix so powerful. The idea is simple: take the output of one command and feed it directly as input to the next command. That is it. But once you start chaining commands together, you can do incredibly complex tasks in a single line.
The pipe operator |
The pipe character | sits between two commands. It sends the stdout of the left command to the stdin of the right command. Think of it as a conveyor belt moving data from one tool to the next.
$ ls | grep ".txt"
notes.txt
todo.txt
data.csv
Try it Yourself โ
Chaining multiple commands
You can chain as many pipes as you want. Each command filters or transforms the data a little bit more. Here is a classic example: list all running processes, search for a specific one, count how many there are, and ignore the grep process itself.
$ ps aux | grep "nginx" | grep -v "grep" | wc -l
4
Each pipe passes the output along. The data flows through a pipeline of small, focused tools. That is the Unix philosophy in action.
Try it Yourself โPractical pipe examples
Here are some real-world pipelines you will use all the time:
$ cat server.log | grep "ERROR" | sort | uniq -c | sort -rn
23 Connection timeout
15 Database unreachable
3 Permission denied
That one line finds all errors, groups and counts them, and shows the most frequent ones first. In a GUI that would take clicking through several menus. In Unix it takes ten seconds to type.
$ history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10
This shows your ten most used commands. It is a great way to discover which commands you should get really good at.
Try it Yourself โ