Pipes (|) in Linux¶
Pipes are one of the most powerful features of Linux. They allow you to connect multiple commands together, where the output of one command becomes the input of another. Instead of creating temporary files, you can build efficient command pipelines for filtering, searching, analyzing, and processing data.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand what a pipe is
- Learn how pipes work internally
- Connect multiple Linux commands
- Filter and process command output
- Build powerful command pipelines
- Use pipes in real-world administration tasks
- Apply pipes in DevOps and Cloud environments
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 Lessons 1–9
Why Learn Pipes?¶
Suppose you have 5,000 running processes.
You don't want to see all of them.
Instead, you only want to find:
- Docker
- Kubernetes
- NGINX
- SSH
Without pipes, this becomes difficult.
With pipes:
One command.
Instant results.
What is a Pipe?¶
A pipe (|) sends the Standard Output (stdout) of one command directly to the Standard Input (stdin) of another command.
Instead of displaying output on the screen, Linux transfers it directly between commands.
How Pipes Work¶
Example:
Workflow:
Basic Pipe Example¶
Without pipe:
Displays:
With pipe:
Output:
Pipe with grep¶
Find running SSH processes.
Pipeline:
Pipe with head¶
Display only the first five files.
Pipe with tail¶
Display the last five files.
Multiple Pipes¶
Linux allows unlimited pipelines.
Example:
Workflow:
Count Matching Results¶
Example:
Pipeline:
Sort Output¶
Example:
Output:
Reverse sort:
Remove Duplicate Entries¶
Example:
Count duplicates:
Example:
Pipe with tee¶
Display output while saving it.
Output:
The same output is saved to:
Pipe with less¶
Large output:
Benefits:
- Scroll
- Search
- Navigate
Ideal for production logs.
Pipe with xargs¶
Suppose you have:
Delete them:
Warning
Use carefully.
Preview first:
Complex Pipeline Example¶
Display running Docker containers.
Workflow:
Another Example¶
Count running Kubernetes Pods.
Real Production Examples¶
Find failed services.
Search logs.
Find listening ports.
Display top memory consumers.
Count active users.
Find SSH login attempts.
Visual Pipeline¶
Command Summary¶
| Pipeline | Purpose |
|---|---|
ls \| wc -l | Count files |
ps -ef \| grep ssh | Search process |
cat file \| sort | Sort data |
sort file \| uniq | Remove duplicates |
history \| grep docker | Search history |
journalctl \| less | Read logs |
find . \| xargs | Process search results |
Production Perspective¶
Pipes are everywhere.
Docker
Kubernetes
Git
Linux
Networking
Monitoring
Every DevOps engineer uses pipelines daily.
Hands-on Lab¶
Task 1¶
Count files.
Task 2¶
Display first five entries.
Task 3¶
Display last five entries.
Task 4¶
Create sample file.
Add:
Press:
Task 5¶
Sort.
Task 6¶
Remove duplicates.
Task 7¶
Count duplicates.
Task 8¶
Search history.
Task 9¶
Save and display output.
Production Troubleshooting Scenario¶
Scenario
An NGINX server is experiencing intermittent failures.
Tasks:
- Find the NGINX process.
- Display only error logs.
- Count the number of error entries.
- Save filtered errors to a file.
- Display and save the output simultaneously.
Solutions:
ps -ef | grep nginx
journalctl | grep ERROR
journalctl | grep ERROR | wc -l
journalctl | grep ERROR > errors.log
journalctl | grep ERROR | tee errors.log
Mini Challenge¶
Create a file named:
Contents:
Now:
- Sort the file.
- Remove duplicates.
- Count unique servers.
- Save sorted output.
- Display the first three entries.
- Display the last two entries.
Use only pipes and commands you've learned.
Best Practices¶
- Use pipes instead of temporary files whenever possible.
- Keep pipelines readable.
- Test each command individually before combining them.
- Use
teewhen you need to both save and view output. - Filter data early to improve performance.
Common Mistakes¶
❌ Creating unnecessary temporary files.
✅ Instead of:
Use:
❌ Building very long pipelines without testing.
✅ Test each stage before combining them.
❌ Forgetting that pipes transfer only stdout.
✅ If you also need stderr, redirect it appropriately.
Interview Questions¶
Beginner¶
- What is a pipe?
- What does the
|symbol do? - What is the difference between a pipe and redirection?
- Which command counts lines?
Intermediate¶
- Explain how pipes work internally.
- Why are pipes preferred over temporary files?
- Explain
sort | uniq. - What does
teedo in a pipeline?
Architect Level¶
- How do pipes simplify Linux automation?
- How would you analyze millions of log entries using pipelines?
- Why are pipelines fundamental to DevOps workflows?
Summary¶
In this lesson, you learned:
- What pipes are
- How pipelines work
- Combining Linux commands
- Filtering and processing command output
- Using
sort,uniq,wc,grep,head,tail, andteewith pipes - Production-ready command pipelines
Pipes are one of Linux's most elegant features. By combining simple commands, you can perform complex tasks efficiently without writing custom programs.
Key Takeaways¶
- Pipes connect the output of one command to the input of another.
- Pipelines eliminate the need for temporary files.
- Combining simple commands creates powerful workflows.
- Pipes are heavily used in Linux administration, DevOps, and Cloud Engineering.
- Mastering pipelines significantly improves productivity and troubleshooting skills.
What's Next?¶
Module 2 Summary — Linux Command Line Essentials
In the next lesson, you'll:
- Review all key concepts from Module 2
- Complete a hands-on assessment
- Test your knowledge with quizzes
- Solve production-focused challenges
- Prepare for Module 3: Text Processing