Practical Redirection Examples
System Logging and Monitoring
1. Log File Management
## Capture system messages
dmesg > system_log.txt
## Append kernel messages
dmesg >> kernel_history.log
2. Error Tracking
## Separate successful and failed commands
find / -name "*.log" 2> error_search.log 1> successful_search.txt
Data Processing Techniques
1. Text File Manipulation
## Sort and redirect content
cat unsorted_data.txt | sort > sorted_data.txt
## Count lines in file
wc -l < input.txt > line_count.txt
## Filter and redirect specific content
grep "ERROR" application.log > error_summary.txt
## Process multiple files
cat file1.txt file2.txt > combined_output.txt
1. System Resource Logging
## Capture system performance metrics
top -n 1 > system_performance.log
## Redirect CPU and memory information
ps aux 2> /dev/null | sort -nrk 3 > cpu_usage.txt
Security and Audit Logging
1. Secure Log Redirection
## Capture authentication attempts
sudo tail /var/log/auth.log > security_audit.txt
## Redirect sensitive command outputs securely
history | grep "sudo" 2> /dev/null > admin_actions.log
Workflow Automation
1. Script Logging
## Redirect script execution logs
./backup_script.sh > backup_log.txt 2>&1
## Silent execution with error tracking
./critical_process.sh > /dev/null 2> error_trace.log
Redirection Strategy Visualization
graph TD
A[Command Execution] --> B{Redirection}
B -->|stdout| C[Normal Output File]
B -->|stderr| D[Error Log]
B -->|Combined| E[Comprehensive Log]
Best Practices in LabEx Linux Environments
Practice |
Recommendation |
Error Handling |
Always redirect stderr |
Log Management |
Use descriptive filenames |
Performance |
Minimize unnecessary I/O operations |
Security |
Protect sensitive log contents |
By mastering these practical redirection techniques, you'll enhance your Linux command-line efficiency and system management skills.