Input, Output, Pipes, and Redirection¶
One of the most powerful features of Linux is the ability to redirect input and output between commands and files. Redirection and pipes allow you to combine simple commands into powerful workflows, making automation, troubleshooting, and data processing much more efficient.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand Standard Input, Output, and Error
- Redirect command output to files
- Append output to existing files
- Redirect errors
- Combine commands using pipes
- Use the
teecommand - Build powerful Linux command pipelines
Prerequisites¶
Before starting this lesson, complete:
- Module 1 – Linux Fundamentals
- Module 2 Lessons 1–8
Why Learn Redirection?¶
Imagine you run a command that produces thousands of lines.
Instead of displaying everything on the screen, you can:
- Save it to a file
- Send it to another command
- Filter the results
- Ignore errors
- Build automation workflows
Almost every shell script and DevOps pipeline uses redirection.
Standard Streams¶
Every Linux process has three standard data streams.
| Stream | Number | Purpose |
|---|---|---|
| Standard Input | 0 | Receives input |
| Standard Output | 1 | Displays normal output |
| Standard Error | 2 | Displays error messages |
Visual representation:
Standard Output (stdout)¶
Example:
Output:
Normally, output is displayed on the terminal.
Redirect Output (>)¶
Save output to a file.
Check:
Output:
Important
If the file already exists:
The existing contents are overwritten.
Append Output (>>)¶
Append instead of overwrite.
Now:
Output:
Standard Error (stderr)¶
Example:
Output:
This message is sent to Standard Error (2).
Redirect Errors¶
Save only errors.
View:
Append errors.
Redirect Output and Errors Together¶
Example:
Both normal output and errors go into one file.
Discard Output¶
Linux provides a special device:
Anything sent here disappears.
Ignore output.
Ignore errors.
Ignore everything.
Standard Input (stdin)¶
Many commands accept input.
Example:
Instead of typing manually, the file becomes the command's input.
Pipes (|)¶
A pipe sends the output of one command directly into another command.
Syntax:
Visual representation:
Pipe Example¶
Display only the first five files.
Count files.
Search for a process.
Sort output.
Count matching lines.
The tee Command¶
Normally:
displays output.
Using:
Result:
- Displays output
- Saves output
Append:
Combining Multiple Pipes¶
Linux allows multiple pipes.
Example:
Workflow:
This is one of Linux's greatest strengths.
Input and Output Flow¶
Real Production Examples¶
Display running Docker containers.
Count Kubernetes Pods.
Find failed services.
Search logs.
Save process list.
Monitor logs while saving.
Production Perspective¶
Every DevOps engineer uses pipes and redirection.
Examples:
Terraform
Kubernetes
Docker
Git
System Administration
Hands-on Lab¶
Task 1¶
Redirect output.
Task 2¶
Append.
Task 3¶
Display.
Task 4¶
Redirect error.
Task 5¶
View errors.
Task 6¶
Count files.
Task 7¶
Display first five entries.
Task 8¶
Use tee.
Task 9¶
Search.
Command Deep Dive¶
| Symbol / Command | Purpose | Example |
|---|---|---|
> | Overwrite output | ls > files.txt |
>> | Append output | date >> log.txt |
2> | Redirect errors | ls missing 2> error.log |
2>> | Append errors | ls missing 2>> error.log |
2>&1 | Combine stdout and stderr | command > all.log 2>&1 |
< | Redirect input | sort < names.txt |
\| | Pipe output | ps -ef \| grep ssh |
tee | Display and save output | date \| tee log.txt |
Production Troubleshooting Scenario¶
Scenario
An application has stopped working.
Tasks:
- Save running processes to a file.
- Search for the application process.
- Count running services.
- Save error logs.
- Monitor logs while writing them to another file.
Solutions:
ps -ef > processes.txt
ps -ef | grep nginx
systemctl list-units | wc -l
journalctl -p err > errors.log
tail -f app.log | tee monitor.log
Mini Challenge¶
Create a file named:
Add:
Perform the following:
- Sort the file.
- Remove duplicate entries.
- Count the number of unique names.
- Save the sorted output to a new file.
- Display the output while saving it.
Hints:
Best Practices¶
- Use
>>when preserving existing data. - Redirect errors to separate log files during troubleshooting.
- Use pipes instead of creating temporary files whenever possible.
- Use
teeto monitor output while saving it. - Understand standard streams before writing shell scripts.
Common Mistakes¶
❌ Accidentally overwriting files.
✅ Use:
Always verify before using.
❌ Ignoring error messages.
✅ Redirect errors to a file for analysis.
❌ Creating unnecessary temporary files.
✅ Use pipes instead.
❌ Forgetting that 2> redirects only errors.
✅ Use:
to capture everything.
Interview Questions¶
Beginner¶
- What are stdin, stdout, and stderr?
- What does
>do? - Difference between
>and>>? - What is a pipe?
Intermediate¶
- Explain
2>and2>&1. - What is
/dev/null? - What does the
teecommand do? - Why are pipes preferred over temporary files?
Architect Level¶
- How do pipes improve Linux automation?
- How would you capture logs and errors separately in production?
- Why is understanding standard streams essential for shell scripting?
Summary¶
In this lesson, you learned:
- Standard Input
- Standard Output
- Standard Error
- Output redirection
- Error redirection
- Pipes
tee- Linux command pipelines
These concepts are fundamental to Linux automation and shell scripting. Almost every Bash script, CI/CD pipeline, and production troubleshooting workflow relies on redirection and pipes.
Key Takeaways¶
- Linux uses three standard streams: stdin (0), stdout (1), and stderr (2).
>overwrites a file, while>>appends to it.2>redirects error messages.|connects the output of one command to another.teedisplays output while simultaneously saving it.- Combining simple commands with pipes creates powerful automation workflows.
What's Next?¶
In the final lesson of Module 2, you'll learn:
- What a pipe is
- How pipes work internally
- Connecting multiple Linux commands
- Filtering and processing command output
- Building powerful command pipelines
- Using pipes in real-world administration and DevOps tasks