How do I save a shell's stderr and stdout to a file while still having it output to the terminal window?

7,287

I usually add 2>&1 | tee -a <filename> to the end of my commands so that stderr is redirected to stdout, then tee displays it onscreen while writing the output to a file.

Share:
7,287

Related videos on Youtube

Anon
Author by

Anon

Specialties: Keyboard Layouts Audiobooks and Text to Speech Qt

Updated on September 18, 2022

Comments

  • Anon
    Anon over 1 year

    I need stdout and stderr to a file, and my shell simultaneously.

    command > file.txt, the usual goto, will not output stderr to the file, nor output stdout to the terminal.

    command > file.txt && cat file.txt will also not work because it needs to be real time, and it needs to have colour codings.

    What command or primitive do I need to utilise in order to be able to read both to the file and my shell, real time?

    • Melebius
      Melebius over 7 years
      If you want stderr and stdout in separate files, follow stackoverflow.com/a/692407/711006.
    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 7 years
      @Akiva Well, the answer there has exactly what you need, even mentioned stderr and has example of usage. IMHO your question is a duplicate even if not word for word.
  • Anon
    Anon over 7 years
    Very cool; mind breaking down the individual commands and arguments? There are a few I'm not familiar with, such as tee.
  • Melebius
    Melebius over 7 years
    @Akiva tee is the command you are looking for, so you are most likely not familiar with it yet. :-)
  • Kyle H
    Kyle H over 7 years
    Correct. For example, if I want to put all terminal output of my ls command, this is what I would type: 'ls 2>&1 | tee -a <filename>'. Ls displays what files are in the current directory, 2>&1 redirects stderr to stdin, pipe forwards stdin to the input of the next command, tee normally displays stdin onscreen while overwriting a file named on the commandline, but with -a it will append/add the output to the end of the file.
  • Anon
    Anon over 7 years
    @KyleH this may be a more of a vim question, but outputting to a log leaves all the terminal artifacts such as colour codes and what have you. Know if it is possible to disable this, or alternatively, to enable vim to interpret the colour codes?
  • Kyle H
    Kyle H over 7 years
    Unfortunately, I don't know the answer to your question, @Akiva. I would look into different commands you can use in vim, such as ':syntax off', 'set list', and other similar ones. I am not sure there is one that will stop interpreting the garbage echoed out into the log, set list would be the closest I would think of.