Skip to content

Q&A · Answered

How to redirect output to file in bash

asked 9 months, 1 week ago 1818 views 1 answer
50

How do I save command output to a file? Also how to redirect errors?

1 Answer

One accepted
27

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago
# Redirect stdout
command > output.txt

# Append instead of overwrite
command >> output.txt

# Redirect stderr
command 2> errors.txt

# Redirect both stdout and stderr
command > output.txt 2>&1
# Or modern syntax
command &> output.txt

# Redirect stderr to stdout
command 2>&1 | tee output.txt

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.