Pipes
Connecting Commands Together
If redirection sends output to a file, the pipe sends output to another command. This is the single most important concept in this entire course.
The pipe operator is the vertical bar character: |
It takes the output of the command on the left and feeds it as input to the command on the right.
command1 | command2
That’s it. The output of command1 becomes the input for command2. Simple idea, massive power.
The pipe is THE defining feature of Unix and Linux. The entire philosophy is built around it: make small tools that each do one thing well, then connect them together with pipes to do complex things. Every AI coding tool that works with the terminal relies on this constantly.
Your First Pipe
Try it now: Type ls ~/projects/my-app | head -5
Here’s what happened:
ls ~/projects/my-appgenerated a list of files- The
|sent that list to the next command head -5took that list and showed only the first 5 lines
Without the pipe, you’d have to run ls, read through all the output, and manually count the first five items. The pipe does it instantly.
Filtering with grep
One of the most common pipe patterns is filtering output with grep.
Try it now: Type cat ~/projects/my-app/logs/error.log | grep "ERROR"
This reads the entire log file, then filters it to show only lines containing the word “ERROR”. Instead of scrolling through hundreds of log lines, you see only the ones that matter.
Try this too: Type history | grep "cd"
This searches through your command history and shows every time you used the cd command. A quick way to find commands you’ve run before.
Counting with wc
Another common pattern: piping into wc -l to count lines.
Try it now: Type ls ~/projects/my-app/src | wc -l
This counts how many files and directories are in the src folder. The ls command generates the list, and wc -l counts the number of lines in that list.
Reading the Pipe Left to Right
When you see a pipe command, read it from left to right like a sentence:
| Command | Plain English |
|---|---|
ls \| head -5 |
List files, then show only the first 5 |
cat file \| grep "error" |
Read the file, then filter for lines with “error” |
history \| grep "cd" |
Show command history, then filter for “cd” commands |
ls src \| wc -l |
List files in src, then count how many there are |
Each | is essentially the word “then” — take the result, then do the next thing with it.
Multiple Pipes
You can chain as many pipes as you want:
cat file.log | grep "ERROR" | head -10
This reads the file, filters for error lines, then shows only the first 10. Each pipe passes the result along to the next command.
Try it now: Type ls -la ~/projects/my-app | grep ".js" | head -3
This lists all files with details, filters for ones with “.js” in the name, then shows only the first 3 matches.
Why AI Tools Love Pipes
When Claude Code, Cursor, or other AI tools need to understand your project, they almost always use pipes. Instead of running five separate commands and reading each output, they chain everything into one efficient line:
- Need to know how many JavaScript files are in a project? One piped command.
- Need to find all the TODO comments? One piped command.
- Need to check if a specific error is in a log? One piped command.
Understanding pipes means you can read exactly what your AI assistant is doing and verify it makes sense before it runs.
Practice
Try these commands in the terminal:
ls ~/projects/my-app | head -5— list the first 5 itemscat ~/projects/my-app/logs/error.log | grep "ERROR"— find errors in the loghistory | grep "ls"— find all the times you usedlsls -la ~/projects/my-app | grep ".json"— find JSON files with detailscat ~/projects/my-app/logs/error.log | grep "ERROR" | head -3— first 3 error lines