Redirection and Pipes
Challenge Gallery
Quick Reference
Redirection operators:
| Operator | Effect |
|---|---|
> file |
Redirect stdout to file (overwrite) |
>> file |
Redirect stdout to file (append) |
< file |
Redirect file to stdin |
2> file |
Redirect stderr to file |
2>&1 |
Redirect stderr to stdout |
&> file |
Redirect both stdout and stderr (bash) |
Pipeline pattern:
producer | filter | filter | consumer
# Example:
cat access.log | grep 404 | sort | uniq -c | sort -rn | head
Common Pitfalls
- Order matters with 2>&1 –
cmd > file 2>&1works (stderr follows stdout to file).cmd 2>&1 > filedoesn’t (stderr goes to terminal). - Overwriting with > –
cmd > important.txtdestroys existing contents. Use>>to append. - Broken pipe – If a downstream command exits early (like
head), upstream gets SIGPIPE. Usually harmless but can cause confusing error messages.