Search Icon

Ryan Harrison My blog, portfolio and technology related ramblings

Bash - Redirecting stdout and stderr

Redirect stdout/stderr to a truncated file:

# StdOut
cmd > out.txt # (stderr printed to console)
# StdErr
cmd 2> err.txt # (stdout printed to console)

Redirect stdout/stderr to a file (appending):

# StdOut
cmd >> out.txt # (stderr printed to console)
# StdErr
cmd 2>> err.txt # (stdout printed to console)

Redirect both stdout and stderr in same command to different files (truncating):

cmd > out.txt 2> err.txt

Redirect both stdout and stderr to same output (truncating):

cmd > out.txt 2>&1

“Redirection statements are evaluated, as always, from left to right. >> file - STDOUT to file (append mode) (short for 1>> file) 2>&1 - STDERR to ‘where stdout goes’ Note that the interpretation ‘redirect STDERR to STDOUT’ is wrong”

Source