Practical Examples
Real-World Redirection Scenarios
Log Management and Analysis
## Collect system logs with timestamp
date >> system_log.txt
journalctl -n 50 >> system_log.txt 2>&1
File Processing and Filtering
## Extract specific data from large files
grep "ERROR" application.log > error_summary.txt
awk '{print $3}' data.csv > filtered_column.txt
Workflow Automation
Batch File Renaming
## Rename files and log the process
for file in *.txt; do
mv "$file" "${file%.txt}_backup.txt" 2>> rename_log.txt
done
Resource Tracking
## Monitor system resources and save report
top -n 1 -b > system_resources.log
free -h >> system_resources.log
Data Processing Pipeline
graph LR
A[Raw Data] --> B[Filter]
B --> C[Sort]
C --> D[Transform]
D --> E[Output File]
## Multi-stage data processing
cat large_dataset.csv \
| grep "valid" \
| sort -t, -k2 -n \
| cut -d, -f3 > processed_data.txt 2> error_log.txt
Security and Audit Logging
Command Execution Tracking
## Record all executed commands
history | tee -a command_history.log
Backup and Archiving
Compressed Backup Creation
## Create compressed backup with error logging
tar -czvf backup.tar.gz /home/user/documents 2> backup_errors.log
Practical Use Cases
| Scenario | Redirection Technique | Purpose |
| ---------------- | --------------------- | --------------- | ------------------- |
| Log Management | >>
| Append logs |
| Error Tracking | 2>
| Capture errors |
| Data Processing | |
| Pipeline operations |
| Silent Execution | > /dev/null
| Suppress output |
Best Practices
- Always handle potential errors
- Use descriptive log files
- Understand stream relationships
Enhance your Linux skills with practical examples from LabEx!