Input and Output redirection allows Linux users to:
- Save command output to files
- Read input from files
- Combine commands
- Redirect errors
- Automate workflows
Standard Streams in Linux
Linux processes use three default streams.
| Stream | Number | Purpose |
|---|---|---|
| STDIN | 0 | Input |
| STDOUT | 1 | Normal output |
| STDERR | 2 | Error output |
1. Output Redirection >
Purpose
Redirect command output into a file.
Syntax
command > file
Example
ls > files.txt
Instead of showing output on terminal:
- Output is saved in:
files.txt
Important Behavior
overwrites file content.
Example
echo "Hello" > test.txt
File content becomes:
Hello
Real-World Usage
Save Process List
ps aux > processes.txt
Export Kubernetes Pods
kubectl get pods > pods.txt
2. Append Output >>
Purpose
Append output to existing file.
Syntax
command >> file
Example
echo "New Log" >> app.log
Adds text to end of file.
Difference Between > and >>
| Operator | Behavior |
|---|---|
| > | Overwrite |
| >> | Append |
Real-World Usage
Append Logs
date >> backup.log
Store Deployment History
kubectl get nodes >> cluster_status.log
3. Input Redirection <
Purpose
Provide file content as input to command.
Syntax
command < file
Example
wc -l < users.txt
Counts lines in file.
Real-World Usage
SQL Script Execution
mysql < database.sql
4. Error Redirection 2>
Purpose
Redirect errors separately.
Syntax
command 2> error.log
Example
ls invalidfile 2> errors.txt
Error saved into:
errors.txt
Real-World Usage
Save Deployment Errors
kubectl apply -f app.yaml 2> deploy_errors.log
5. Redirect STDOUT and STDERR Together
Syntax
command > output.log 2>&1
Meaning
| Part | Purpose |
|---|---|
| > | Redirect stdout |
| 2>&1 | Redirect stderr to stdout |
Example
docker ps > docker.log 2>&1
Both:
- Normal output
- Errors
go into same file.
Modern Shortcut
command &> file.log
6. Pipe |
Purpose
Send output of one command as input to another.
Syntax
command1 | command2
Example
ls -l | less
Another Example
ps aux | grep nginx
Process:
- ps aux shows processes
- Output passed to grep
- grep filters nginx processes
Real-World Usage
Kubernetes
kubectl get pods | grep running
Docker
docker ps | grep nginx
7. Tee Command
Purpose
Display output and save to file simultaneously.
Syntax
command | tee file
Example
ls | tee output.txt
Output:
- Displays on terminal
- Saves in file
Real-World Usage
Save Deployment Logs
kubectl apply -f app.yaml | tee deploy.log
Practical DevOps Examples
Monitor Logs
tail -f app.log | grep ERROR
Save System Information
df -h > disk_usage.txt
Store Failed Services
systemctl status nginx 2> nginx_errors.log
Backup Kubernetes Output
kubectl get all > cluster_backup.txt
Common Redirection Symbols
| Symbol | Purpose |
|---|---|
| > | Redirect output |
| >> | Append output |
| < | Redirect input |
| 2> | Redirect errors |
| tee | Save and display output |
Dangerous Mistake
Accidentally Overwriting File
echo "test" > important.txt
This removes old content.
Use:
>>
if appending is needed.
Top comments (0)