218

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?

jww
  • 97,681
  • 90
  • 411
  • 885
user784637
  • 15,392
  • 32
  • 93
  • 156

5 Answers5

358

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

Sujoy
  • 8,041
  • 3
  • 30
  • 36
  • 8
    Great answer! I really like your explanation of how `1>` can be written as `>` – user784637 Oct 26 '11 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) – ThorSummoner Jan 19 '15 at 05:19
  • 4
    @ThorSummoner http://www.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. – DreadPirateShawn Sep 02 '15 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. http://wiki.bash-hackers.org/howto/redirection_tutorial is a *far* better reference source on redirection. – Charles Duffy Jun 06 '18 at 20:01
  • Shouldn’t standard output be always redirected first, before standard error is redirected?? – codepoet Oct 01 '21 at 22:30
  • @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 first – Sujoy Dec 28 '21 at 14:23
  • 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. – Thomas Jay Rush Oct 29 '22 at 18:03
40

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.

Quintus.Zhou
  • 1,013
  • 10
  • 13
12

Like that:

$ command >>output 2>>error
Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
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
ztank1013
  • 6,939
  • 2
  • 22
  • 20
  • 38
    This is not an answer to the question. – Matthias Mar 11 '15 at 13:34
  • Why do people merge outputs or suggest merging outputs? – nurettin Oct 07 '18 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. – streamofstars May 22 '19 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 '19 at 06:59
0

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.

rahogaboom
  • 61
  • 3