Practical Redirection Examples
Log Management
System Log Processing
## Capture system logs with specific filtering
dmesg | grep "error" > system_errors.log
## Append kernel messages to log file
dmesg >> kernel_log.txt
Data Processing
File Content Manipulation
## Sort and save file contents
sort input.txt > sorted_output.txt
## Count lines in file
wc -l file.txt > line_count.txt
Backup and Monitoring
Automated Backup Script
#!/bin/bash
## LabEx backup script example
tar -czvf backup.tar.gz /home/user/documents 2> backup_errors.log
Resource Tracking
## Redirect system resource information
top -n 1 > system_resources.txt
df -h 2>> disk_usage.log
Error Handling
Silent Execution
## Run command silently, log errors
./script.py > /dev/null 2>> error_log.txt
Redirection Workflow
graph LR
A[Command Execution] --> B{Output Type}
B -->|Normal Output| C[Stdout Redirection]
B -->|Error Messages| D[Stderr Redirection]
B -->|Combined| E[Combined Redirection]
Common Redirection Patterns
| Scenario | Redirection Technique | Example |
| --------- | ----------------------- | ---------------------------------- | ------------- |
| Logging | Append errors | command 2>> error.log
|
| Filtering | Pipe with grep | command | grep pattern
|
| Backup | Redirect with timestamp | command > backup_$(date +%F).txt
|
By understanding these practical redirection techniques, shell script developers can create more robust and efficient scripts for system administration, data processing, and monitoring tasks in LabEx environments.