How to redirect stderr and stdout to different files in the same line in script?
Asked Answered
S

5

224

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?

Sankhya answered 26/10, 2011 at 10:36 Comment(0)
P
367

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

Pyemia answered 26/10, 2011 at 10:38 Comment(7)
Great answer! I really like your explanation of how 1> can be written as >Sankhya
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)Hullabaloo
@Hullabaloo 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.Omura
@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.Imparipinnate
Shouldn’t standard output be always redirected first, before standard error is redirected??Manic
@reportaman - The order does not matter, unless there you are redirecting one descriptor to another for example 2>&1, In this case you need to redirect 1 firstPyemia
The next previous question is correct (at least on Mac). You have to put the redirection of stderr (i.e. 2) after the redirection of stdout. At least I do, at least on Mac. It took me hours to figure this out.Materialism
P
44

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.

Phyllode answered 10/4, 2014 at 5:48 Comment(1)
Thanks.i searching this for one hourEnroll
T
12

Like that:

$ command >>output 2>>error
Trella answered 26/10, 2011 at 10:38 Comment(0)
E
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
Ed answered 26/10, 2011 at 11:47 Comment(4)
This is not an answer to the question.Latrinalatrine
Why do people merge outputs or suggest merging outputs?Paley
@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.Tablespoon
@Tablespoon 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.Paley
H
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.

Honeyman answered 15/5, 2020 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.