Understanding Linux Redirection
What Is Redirection in Simple Terms
Every process has three standard streams: stdin (input), stdout (output), and stderr (errors). By default, stdin reads from the keyboard and stdout/stderr print to the terminal. Redirection changes these defaults — pointing streams at files, devices, or other commands instead.
How It Works
◈ DIAGRAM
+------------------------------------------+| File descriptor table for any process || || 0 = stdin <- keyboard (default) || 1 = stdout -> terminal (default) || 2 = stderr -> terminal (default) |+------------------------------------------+ Redirection changes where each FD points: command > file.txt FD 1 now points to file.txt instead of terminal command 2>&1 FD 2 now points wherever FD 1 currently pointsComplete redirection reference:
Bash
## Redirect stdout to file (overwrites)command > output.txtcommand 1> output.txt ## same (1 is stdout FD) ## Append stdout to filecommand >> output.txt ## Redirect stderr to filecommand 2> errors.txt ## Redirect stderr to stdout (combine both streams)command 2>&1 ## order matterscommand > all.txt 2>&1 ## both go to filecommand &> all.txt ## shorthand for above (bash only) ## Discard stdoutcommand > /dev/null ## Discard all outputcommand > /dev/null 2>&1command &> /dev/null ## bash shorthand ## Read stdin from filecommand < input.txt ## Here-document (inline stdin)cat << 'EOF'line 1line 2EOF ## Here-string (single string as stdin)grep pattern <<< "search in this string" ## Tee: write to file AND pass to next commandcommand | tee output.txt | next-command ## Tee appendcommand | tee -a output.txtPractical Commands
Bash
## Log script output to file while also showing on terminal./deploy.sh 2>&1 | tee /var/log/deploy-$(date +%Y%m%d).log ## Separate stdout and stderr logs./build.sh > /var/log/build.log 2> /var/log/build-errors.log ## Suppress error output from a commandfind / -name 'config.yaml' 2>/dev/null ## Read file line by linewhile IFS= read -r line; do echo "Processing: $line"done < /etc/hosts ## Write to a file requiring root from a non-root scriptecho 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf## tee runs as sudo, so it can write the file## Without tee: sudo echo ... >> /etc/sysctl.conf FAILS## (>> is processed by the shell BEFORE sudo runs)Troubleshooting
| Symptom | Command | What to Check |
|---|---|---|
| Error output not captured | Add 2>&1 |
Only stdout redirected, not stderr |
| sudo echo fails to write | Use sudo tee |
Shell handles >> before sudo |
| Order of 2>&1 matters | cmd 2>&1 > file vs cmd > file 2>&1 |
Wrong order sends stderr to terminal |
COMMON MISTAKE / WARNING**Common Mistake:** Writing `command 2>&1 > file.txt` expecting both stdout and stderr to go to the file. This is wrong. `2>&1` redirects stderr to wherever stdout currently points (the terminal), THEN `> file.txt` redirects stdout to the file. The correct order is `command > file.txt 2>&1`.
PLACEMENT PRO TIP**Tip:** Use `sudo tee` when you need to write to a root-owned file from a script. `echo 'content' | sudo tee /etc/protected-file` works because `tee` runs as sudo. The alternative `sudo echo 'content' >> /etc/protected-file` fails — the shell processes `>>` before elevating to sudo.