This question already has an answer here:
I know that in Linux, to redirect output from the screen to a file, I can either use the >
or tee
. However, I'm not sure why part of the output is still output to the screen and not written to the file.
Is there a way to redirect all output to file?
To get the output on the console AND in a file
file.txt
for example.Note:
&
(in2>&1
) specifies that1
is not a file name but a file descriptor.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:
If you want to silence the error, do:
Use
>>
to append:command >> file
Use this -
"require command here" > log_file_name 2>&1
Detail description of redirection operator in Unix/Linux.
The > operator redirects the output usually to a file but it can be to a device. You can also use >> to append.
If you don't specify a number then the standard output stream is assumed but you can also redirect errors
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.