Redirect all output to file in Bash [duplicate] Redirect all output to file in Bash [duplicate] linux linux

Redirect all output to file in Bash [duplicate]


That part is written to stderr, use 2> to redirect it. For example:

foo > stdout.txt 2> stderr.txt

or if you want in same file:

foo > allout.txt 2>&1

Note: this works in (ba)sh, check your shell for proper syntax


All POSIX operating systems have 3 streams: stdin, stdout, and stderr. stdin is the input, which can accept the stdout or stderr. stdout is the primary output, which is redirected with >, >>, or |. stderr is the error output, which is handled separately so that any exceptions do not get passed to a command or written to a file that it might break; normally, this is sent to a log of some kind, or dumped directly, even when the stdout is redirected. To redirect both to the same place, use:

$command &> /some/file

EDIT: thanks to Zack for pointing out that the above solution is not portable--use instead:

$command > file 2>&1 

If you want to silence the error, do:

$command 2> /dev/null


To get the output on the console AND in a file file.txt for example.

make 2>&1 | tee file.txt

Note: & (in 2>&1) specifies that 1 is not a file name but a file descriptor.