Standard streams are three default communication channels between a Linux program and its environment. They provide a standardized way for programs to receive input, display output, and report errors. Understanding streams is fundamental for command-line operations, scripting, and system administration.
Key Concepts
- stdin (0): Standard input - where programs read data
- stdout (1): Standard output - where normal output goes
- stderr (2): Standard error - where error messages go
- File Descriptors: Numeric identifiers (0, 1, 2) for streams
- Redirection: Changing where streams point using operators
Command Syntax
Redirection operators work with any command:
>- Redirect stdout to file (overwrite)>>- Redirect stdout to file (append)<- Redirect stdin from file2>- Redirect stderr to file&>- Redirect both stdout and stderr
Common Options
> - Overwrite file with stdout
>> - Append stdout to file
< - Read stdin from file
2> - Redirect stderr only
2>&1 - Redirect stderr to stdout
| - Pipe stdout to next command
/dev/null - Discard output (black hole)
Practical Examples
Example 1: Basic Output Redirection
|
|
Saves directory listing to file instead of displaying on screen
Example 2: Append to File
|
|
Adds lines to existing file without overwriting
Example 3: Input Redirection
|
|
Feeds file content as input to commands
Example 4: Error Redirection
|
|
Captures error messages separately from normal output
Example 5: Combine stdout and stderr
|
|
Both methods redirect all output to same file
Use Cases
- Logging: Capture command output for later analysis
- Batch Processing: Feed data from files to commands
- Error Handling: Separate normal output from errors
- Silent Operations: Suppress unwanted output
- Automation: Redirect streams in scripts and cron jobs
Related Commands
tee - Write output to file AND display on screen
cat - Display file contents or concatenate
head/tail - Show beginning/end of streams
grep - Filter stream content
sort - Sort input stream
wc - Count lines, words, characters in stream
Tips & Troubleshooting
Common Issues
- Permission denied: Check write permissions for output files
- File not found: Verify input file paths exist
- Disk full: Monitor space when redirecting large outputs
- Overwrites: Use
>>instead of>to preserve existing data
Best Practices
- Always redirect errors in scripts:
command 2>/dev/null - Use absolute paths in automated scripts
- Test redirections with small data first
- Combine with
teewhen you need both file and screen output
Advanced Techniques
|
|