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.

40–50 min Week 1 · Terminal basics Ubuntu terminal
Your outcome: You can direct command output intentionally instead of feeling surprised by files appearing or changing.
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 folder

Demonstrates 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 folder

Sends 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 folder

Creates 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

  1. Create out.txt and err.txt from a command group.
  2. 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.

  1. What does stderr number 2 mean?
  2. Which redirect operator appends?

Your practice, your pace

Loading saved progress…

Discussion

Continue the conversation about this guide. Loading comments connects to GitHub Discussions through giscus.

Open community discussions on GitHub

Check before you copy

This command needs care. Read its context, confirm the target and location, and keep a recovery path.

Copying does not run a command.