Output Redirection

Instead of displaying output on screen, send it to a file.

Basic Redirection: >

Terminal
$echo 'hello world' > greeting.txt
$cat greeting.txt
hello world

The > operator sends stdout to a file, creating it if needed.

Overwrite vs Append

Terminal
$echo 'line 1' > file.txt
$echo 'line 2' > file.txt
$cat file.txt
line 2

> overwrites the file completely!

Terminal
$echo 'line 1' > file.txt
$echo 'line 2' >> file.txt
$cat file.txt
line 1 line 2

>> appends to the file.

Careful with >

> destroys existing content. Always think twice before using it on important files. When in doubt, use >>.

Prevent Overwrites: noclobber

Enable noclobber to prevent accidental overwrites:

hljs bash
set -o noclobber    # Enable protection
echo "text" > existing.txt   # Error: cannot overwrite
echo "text" >| existing.txt  # Force overwrite with >|
set +o noclobber    # Disable protection

Add set -o noclobber to your .bashrc if you want permanent protection.

Redirect stderr

Terminal
$ls /nonexistent 2> errors.txt
$cat errors.txt
ls: cannot access '/nonexistent': No such file or directory

2> redirects stderr (file descriptor 2).

Redirect Both Streams

To Different Files

Terminal
$ls /home /nonexistent > stdout.txt 2> stderr.txt
$cat stdout.txt
/home: user
$cat stderr.txt
ls: cannot access '/nonexistent': No such file or directory

To Same File

Terminal
$command > all.txt 2>&1
(both streams to all.txt)
$command &> all.txt
(shorthand, same result)

Discard Output: /dev/null

/dev/null is a black hole. Anything written to it disappears.

Terminal
$ls > /dev/null
(no output - discarded)
$command 2> /dev/null
(errors discarded, output shown)
$command &> /dev/null
(everything discarded - silent)

Silent Commands

command &> /dev/null is useful when you only care about the exit code, not the output.

Save and Display: tee

What if you want to save output AND see it?

Terminal
$ls | tee listing.txt
Documents Downloads projects (also saved to listing.txt)

We'll cover tee in detail later.

Real-World Examples

Save Command Output

Terminal
$ps aux > processes.txt

Append to Log

Terminal
$echo "$(date): Task completed" >> task.log

Capture Errors

Terminal
$make build 2> build_errors.log

Run Silently

Terminal
$apt-get update > /dev/null 2>&1

A Common Gotcha

This doesn't work as expected:

hljs bash
cat file.txt > file.txt    # WRONG - empties the file!

The shell opens file.txt for writing (emptying it) before cat can read it.

Solution: use a temp file or sponge from moreutils.

Knowledge Check

What's the difference between > and >>?

Quick Reference

SyntaxEffect
> fileRedirect stdout, overwrite
>> fileRedirect stdout, append
2> fileRedirect stderr
2>> fileAppend stderr
&> fileRedirect both (overwrite)
&>> fileRedirect both (append)
> /dev/nullDiscard output

Key Takeaways

  • > redirects stdout to file (overwrites)
  • >> appends instead of overwriting
  • 2> redirects stderr
  • &> redirects both stdout and stderr
  • /dev/null discards output silently
  • Be careful with > - it destroys existing content

Next: input redirection with <.