jgoenetxea
10/18/2018 - 2:28 PM

How to redirect terminal to file

How to redirect command output to a file

It is possible, just redirect the output to a file:

$ command > outputfile.txt  

Or if you want to append data:

$ command >> outputfile.txt 

If you want stderr as well use this:

$ command &> outputfile.txt   

or this to append:

$ command &>> outputfile.txt   

If you want to have both stderr and output displayed on the console and in a file use 'tee' command and pipe the output to it:

$ command | tee outputfile.txt

A slight modification will catch stderr as well:

$ command 2>&1 | tee outputfile.txt

or slightly shorter and less complicated:

$ command |& tee outputfile.txt

To summarize:

          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file   
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
----------||----------|----------||----------|----------||----------
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
----------||----------|----------||----------|----------||----------
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
----------||----------|----------||----------|----------||----------
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
----------||----------|----------||----------|----------||----------
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    ||  append
----------||----------|----------||----------|----------||----------
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append
----------||----------|----------||----------|----------||----------
  • Please note that the n.e. in the syntax column means "not existing".

** There is a way, but it's too complicated to fit into the column. You can find a helpful link in the List section about it.