You can redirect the output stream and (or) errors to a file with overwriting it:
1>file will output data from the output stream to a file with its creation or rewriting.
2>file will output data from the error stream to the file with its creation or rewriting.
1&>file (and several other options) will output data from both the output stream and the error stream to a file with its creation and rewriting.

You can output the output stream or the error stream to a file by writing at its end:

 1>>file 2>>file 

But if you try to do something like 1&>>file , a syntax error will pop up.

How can I write to the end of the file from both the output stream and the error stream?

  • Thank. Strangely enough, I tried a similar variant: 2 &> 1 >> file, and an error message was returned. - Andrey Solodovnikov
  • Nothing strange, if you make a mistake, an error message is displayed. And correct the question - what OS, what shell? - 0xdb

1 answer 1

For example in Bash like this:

 $ { echo "stdout text"; echo "stderr text">&2; } >> file 2>&1 $ cat file stdout text stderr text 

Bash redirects from left to right:

  1. >>file : opens file in record mode and redirects standard output ( stdout )
  2. 2>&1 : redirects error output ( stderr ) to where the standard output is currently output, i.e. to the previously opened file file .

cmd 2>&1 >> file redirects only standard output to file, because error output will be redirected to standard output before the latter is redirected to the file:

 $ { echo "stdout text"; echo "stderr text">&2; } 2>&1 >> file stderr text $ cat file stdout text