Andrew Tomazos
Andrew Tomazos

Reputation: 68738

Bash command for >stdout and >stdout+stderr

Under Linux/bash I want to run a command and send standard output to foo.txt as well as combined standard output and standard error to bar.txt:

$ cmd < input.txt  1>foo.txt 1+2>bar.txt   ???

What's the easiest way to do this?

To send just stdout is:

$ cmd > foo.txt

To send both stdout/stderr is:

$ cmd &> bar.txt

However trying to combine:

$ cmd > foo.txt &>bar.txt

Causes foo.txt to be empty.

Upvotes: 1

Views: 477

Answers (2)

NPE
NPE

Reputation: 500903

The following should do it:

(cmd | tee out.txt) &> both.txt

That'll redirect stdout to out.txt and both stdout and stderr to both.txt.

Upvotes: 1

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 71009

You can not have two redirections of the output stream at the same time. Easiest way to do what you want is to pipe output to the tee command.

Upvotes: 1

Related Questions