I/O Redirection
Control where commands read from and write to.
Basic Redirections
Output (>)
Redirect stdout to file (overwrites):
echo "Hello" > output.txt
ls -la > filelist.txt
Append (>>)
Append to file:
echo "Line 1" > log.txt
echo "Line 2" >> log.txt
Input (<)
Read from file:
cat < input.txt
wc -l < file.txt
sort < unsorted.txt > sorted.txt
Error Redirection
Stderr (2>)
ls /nonexistent 2> errors.txt
command 2>> errors.log # Append
Stderr to Stdout (2>&1)
command 2>&1 | grep error
make 2>&1 | tee build.log
Both Streams (&>)
command &> all.log
# Same as: command > all.log 2>&1
Common Patterns
Discard Output
command > /dev/null # Discard stdout
command 2> /dev/null # Discard stderr
command &> /dev/null # Discard both
Logging
./script.sh &> script.log
./daily.sh &>> daily.log # Append
Separate Streams
command > output.txt 2> errors.txt
Processing Files
tr '[:lower:]' '[:upper:]' < input.txt > output.txt
sort < data.txt > sorted.txt
Order Matters
# Both to file
command > file 2>&1
# Both to pipe
command 2>&1 | grep error
File Descriptors
0= stdin1= stdout2= stderr
2> file # Redirect fd 2
1> file # Same as > file
0< file # Same as < file
Troubleshooting
Permission Denied
Check file/directory permissions.
Accidental Overwrite
Use >> to append, or check first:
test -f file.txt && echo "File exists!"
Limitations
- No heredoc (
<< EOF) - No here-string (
<<< "string") - No process substitution (
<(command)) - No noclobber protection