56

I want to see the output of a command in the terminal as if there was no redirection. Also, stderr needs to be redirected to err.log and stdout needs to be redirected to stdout.log.

It would be nice to also have the exact copy of what is shown in terminal, i.e. errors printed as and when it occurs, in a separate file: stdouterr.log.

1

3 Answers 3

65

Use the tee command as follows:

(cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log

3>&1 1>&2 2>&3 is how you swap stderr and stdout, because tee can only accept stdout.

Take a look at Unix tee command for more advanced redirections using tee.

8
  • 2
    nice solution. Is there any way to get cmd exit code?
    – turbanoff
    Commented Jun 19, 2015 at 16:56
  • 3
    @turbanoff Replace cmd with (cmd ; echo >exit_code.txt $?). Commented Aug 13, 2015 at 21:04
  • 1
    I believe this should better-preserve the order of things output to the command line: ((cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log)
    – TTT
    Commented May 7, 2019 at 17:12
  • 2
    This solution fails to address: "I want to see the output of a command in the terminal as if there was no redirection." There is a race condition and thus lines output to terminal will be randomly ordered.
    – Quasímodo
    Commented Apr 26, 2022 at 0:50
  • 1
    Unix tee command link doesn't work
    – msciwoj
    Commented Aug 12, 2022 at 8:16
8

I think logging stdout and stderr to two different files is a splendid idea. Does it not make the logs asynchronous? So I tried out the following:

  • stdout to "stdout.log" (as dogbane suggested)
  • stderror to "stderr.log" (as dogbane suggested)
  • all output to "all.log" and
  • still be able to see the output on the display (in a separate terminal though!)

((cmd | tee stdout.log) 3>&1 1>&2 2>&3 | tee stderr.log) &> all.log

in another terminal

tail -f --sleep-interval=2 all.log
7
  • 1
    Is it not possible to direct stderr directly to a second tty? Then no logfile is required.
    – Steven Lu
    Commented Apr 25, 2013 at 18:40
  • 1
    @StevenLu yes, if you know the name and have permission to write to the second tty that can be done.
    – Jasen
    Commented Dec 14, 2015 at 23:46
  • 2
    easier would be &| tee all.log on the end of the command instead of &> all.log
    – Jasen
    Commented Dec 14, 2015 at 23:49
  • 2
    @Cbhihe as far as I can tell it does nothing, I meant to say |&
    – Jasen
    Commented Feb 7, 2016 at 19:05
  • 2
    This solution fails to address: "I want to see the output of a command in the terminal as if there was no redirection." There is a race condition and thus lines output to terminal will be randomly ordered.
    – Quasímodo
    Commented Apr 26, 2022 at 0:50
3

@dogbane, Thanks.
I found another way too which saves both the streams approximately in the order as they would get printed without redirection.

command 2> >(tee errlog | tee -a bothLog > /dev/tty ) | tee outlog | tee -a bothLog

But this works only with the shells which supports process substitution.

1
  • 1
    This solution fails to address: "I want to see the output of a command in the terminal as if there was no redirection." There is a race condition and thus lines output to terminal will be randomly ordered.
    – Quasímodo
    Commented Apr 26, 2022 at 0:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .