Mission 6 of 30
Redirection Operators
Capture useful output and errors in separate files.
Learn stdin, stdout, stderr, overwrite, append, heredocs, and the safe black hole /dev/null.
Your outcome: You can direct command output intentionally instead of feeling surprised by files appearing or changing.
Practice inside
Practice inside
~/linux-practice. Check your setup before you begin.
Prerequisite: Viewing Larger Files
.
What you’ll practice
- Redirect normal output and errors
- Use heredocs
- Understand /dev/null
The mental model
- stdin is input, stdout is normal output, stderr is error output.
- > overwrites and >> appends.
- 2> captures error messages.
Try it in your terminal
Overwrite and append
Safe in practice folderDemonstrates the difference between replace and append.
Run from ~/linux-practice
cd ~/linux-practice
echo 'First' > output.txt
echo 'Second' >> output.txt
cat output.txt Capture errors
Safe in practice folderSends stderr to a file instead of the screen.
Run from ~/linux-practice
ls missing-file 2> error.log
cat error.log Use a heredoc
Safe in practice folderCreates multi-line text safely.
Run from ~/linux-practice
cat << 'EOF' > document.txt
Line 1
Line 2
Variables like $HOME are not expanded here
EOF
cat document.txt Put it into practice
- Create out.txt and err.txt from a command group.
- Send an error to /dev/null and notice it disappears.
Check your result
Standard output and errors are stored in the intended files.
Common mistakes
- You run `ls important-file > notes.txt` and now notes.txt is empty. The `>` operator overwrites without asking. If the file had anything in it, it's gone. Get in the habit of checking what's in your target file before redirecting into it.
- You type `2>` thinking it redirects normal output. It doesn't. `>` captures stdout — the regular output. `2>` captures stderr — the error messages. They're two different streams. Mix them up and you'll wonder why your error log is empty.
Check your understanding
Explain these in your own words. If you’re unsure, revisit the examples before moving on.
- What does stderr number 2 mean?
- Which redirect operator appends?
Your practice, your pace
Loading saved progress…