How to redirect output to file in bash

Answered
Jan 05, 2026 1339 views 1 answers
50

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

H
Asked by hassan_ops
Bronze 565 rep

1 Answer

27
# 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
P
Answered by python_dev 1 week, 2 days ago
Platinum 447 rep

Your Answer

You need to be logged in to answer questions.

Log In to Answer