222

I know this much:

$ command 2>> error

$ command 1>> output

Is there any way I can output the stderr to the error file and output stdout to the output file in the same line of bash?

5 Answers 5

361

Just add them in one line command 2>> error 1>> output

However, note that >> is for appending if the file already has data. Whereas, > will overwrite any existing data in the file.

So, command 2> error 1> output if you do not want to append.

Just for completion's sake, you can write 1> as just > since the default file descriptor is the output. so 1> and > is the same thing.

So, command 2> error 1> output becomes, command 2> error > output

7
  • 8
    Great answer! I really like your explanation of how 1> can be written as >
    – user784637
    Oct 26, 2011 at 13:25
  • How is this different from like command &2>err.log, I think i am totally confusing sintaxies. (A link to an appropriate answer of all the bash pipe-isms might be in order) Jan 19, 2015 at 5:19
  • 4
    @ThorSummoner tldp.org/LDP/abs/html/io-redirection.html is what I think you're looking for. Fwiw, looks like command &2>err.log isn't quite legit -- the ampersand in that syntax is used for file descriptor as target, eg command 1>&2 would reroute stdout to stderr. Sep 2, 2015 at 16:32
  • 1
    @DreadPirateShawn, please don't link the ABS as a reference -- it occasionally contains outright inaccuracies, and very frequently contains bad-practice examples. wiki.bash-hackers.org/howto/redirection_tutorial is a far better reference source on redirection. Jun 6, 2018 at 20:01
  • Shouldn’t standard output be always redirected first, before standard error is redirected??
    – codepoet
    Oct 1, 2021 at 22:30
43

Try this:

your_command 2>stderr.log 1>stdout.log

More information

The numerals 0 through 9 are file descriptors in bash. 0 stands for standard input, 1 stands for standard output, 2 stands for standard error. 3 through 9 are spare for any other temporary usage.

Any file descriptor can be redirected to a file or to another file descriptor using the operator >. You can instead use the operator >> to appends to a file instead of creating an empty one.

Usage:

file_descriptor > filename

file_descriptor > &file_descriptor

Please refer to Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection.

1
  • Thanks.i searching this for one hour Mar 14, 2020 at 18:21
12

Like that:

$ command >>output 2>>error
8

Or if you like to mix outputs (stdout & stderr) in one single file you may want to use:

command > merged-output.txt 2>&1
4
  • 38
    This is not an answer to the question.
    – Matthias
    Mar 11, 2015 at 13:34
  • Why do people merge outputs or suggest merging outputs?
    – nurettin
    Oct 7, 2018 at 10:08
  • @nurettin: maybe you a have a script line that just executes a command and instantly saves the output to a log file. The command in question may fail sometimes so you want to save any errors about that too but to the same log file. May 22, 2019 at 17:27
  • @streamofstars yes of course you are right maybe sometimes that is what people want, I was commenting within the context of this question as someone who was seeking answers and found a lot of irrelevant ones all over stackoverflow.
    – nurettin
    May 23, 2019 at 6:59
1

Multiple commands' output can be redirected. This works for either the command line or most usefully in a bash script. The -s directs the password prompt to the screen.

Hereblock cmds stdout/stderr are sent to seperate files and nothing to display.

sudo -s -u username <<'EOF' 2>err 1>out
ls; pwd;
EOF

Hereblock cmds stdout/stderr are sent to a single file and display.

sudo -s -u username <<'EOF' 2>&1 | tee out
ls; pwd;
EOF

Hereblock cmds stdout/stderr are sent to separate files and stdout to display.

sudo -s -u username <<'EOF' 2>err | tee out
ls; pwd;
EOF

Depending on who you are(whoami) and username a password may or may not be required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.