Lesson 21 / 29

Redirection

Sending stdout and stderr wherever you want.

Two output streams

Every process has stdout (normal output) and stderr (error output), plus stdin for input. Redirection lets you send these to files instead of the screen.

>, >> and 2>

> overwrites a file with stdout, >> appends, 2> captures stderr separately.

ls /etc > listing.txt        # overwrite
ls /etc >> listing.txt       # append
ls /nope 2> errors.txt       # stderr only

Output:

cat errors.txt
# ls: cannot access '/nope': No such file or directory

Discard output

/dev/null is a black hole — redirect unwanted output there: command > /dev/null 2>&1 silences everything.