How do I save the output of a command to a file?

Is there a way without using any software? I would like to know how.

share|improve this question
up vote 261 down vote accepted

Yes it is possible, just redirect the output to a file:

someCommand > someFile.txt  

Or if you want to append data:

someCommand >> someFile.txt

If you want stderr too use this:

someCommand &> someFile.txt  

or this to append:

someCommand &>> someFile.txt  
share|improve this answer
4  
Note that someCommand 2> someFile.txt and someCommand 2>> someFile.txt also redirects stterr to someFile.txt – Slothworks Aug 29 '15 at 13:32
    
I'm trying to do this with gcc command but it doesn't work. It works with other commands, but not this one. It simply creates the output file with nothing inside it. – Nik-Lz Sep 29 '16 at 13:40
    
@RestlessC0bra does the tee answers below work? – Seth Sep 29 '16 at 14:06
    
@Seth I'm on windows (Mingw GCC) so no, i don't have this command tee. – Nik-Lz Sep 29 '16 at 14:55
    
@Nik-Lz Often this is because the command is sending all its output on stderr. If gcc is generating error messages, this seems likely. See Slothworks comment for how to capture stderr instead of stdout. – Jonathan Hartley Sep 14 '17 at 13:29

To write the output of a command to a file, there are basically 10 commonly used ways.

Overview:

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.

          || 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

List:

  • command > output.txt

    The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

  • command >> output.txt

    The standard output stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

  • command 2> output.txt

    The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, it gets overwritten.

  • command 2>> output.txt

    The standard error stream will be redirected to the file only, it will not be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

  • command &> output.txt

    Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, it gets overwritten.

  • command &>> output.txt

    Both the standard output and standard error stream will be redirected to the file only, nothing will be visible in the terminal. If the file already exists, the new data will get appended to the end of the file..

  • command | tee output.txt

    The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, it gets overwritten.

  • command | tee -a output.txt

    The standard output stream will be copied to the file, it will still be visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

  • (*)

    Bash has no shorthand syntax that allows piping only StdErr to a second command, which would be needed here in combination with tee again to complete the table. If you really need something like that, please look at "How to pipe stderr, and not stdout?" on Stack Overflow for some ways how this can be done e.g. by swapping streams or using process substitution.

  • command |& tee output.txt

    Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, it gets overwritten.

  • command |& tee -a output.txt

    Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

share|improve this answer
16  
Thanks for the table, it's excellent! This should be top answer – DevShark Aug 15 '16 at 16:24
    
Great information! Can you also add the details of 2>&1 / 1>&2 / 3>&1 ? – karthick87 Sep 19 '16 at 16:30
    
@karthick87 This is not really related to the question about redirecting output to a file, because it just redirects one stream to another. 2>&1 redirects STDERR to STDOUT, 1>&2 redirects STDOUT to STDERR and 3>&1 would redirect stream 3 to STDERR. – Byte Commander Sep 19 '16 at 16:42
3  
Just a note that '|&' wasn't working for me on macOS. This is due to it having an older version of bash (I think). The less elegant '2>&1 |' works fine though – Danny Parker May 9 '17 at 10:01

You can also use tee to send the output to a file:

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

tee is useful if you want to be able to capture command output while also viewing it live.

share|improve this answer
8  
tee is useful if you want to be able to capture command output while also viewing it live. Make this line bold Aaron. It will do two jobs at a time. Thank you for the answer. – learner Nov 2 '15 at 12:26
    
It says that the & is unexpected, and doesn't write the log at the same time as the command runs. I am using this in a bash file however, does that make any difference? – tim687 Apr 6 '16 at 13:51
    
@tim687 I have removed that edit. Sorry about that...wasn't a part of my original answer. – Aaron Apr 6 '16 at 14:11
    
@Aaron Thanks! tee will append the file in real time, right? I have a backup script that I use to,lol , backup my pc, but the logging is not in real time. My pc goes to sleep after the backup is finished, and the log file is empty. Should I use another command to log the commands? – tim687 Apr 7 '16 at 6:43
1  
billion thanks bro – Mahesha999 Jul 15 '16 at 8:33

You can redirect the command output to a file:

your_command >/path/to/file

To append the command output to a file instead of overwriting it, use:

your_command >>/path/to/file
share|improve this answer
    
Thanks a lot ! is there any Limits ? like the max size of the file ? – led-Zepp Feb 14 '14 at 19:55
3  
The max file size is just limited through the file system – chaos Feb 14 '14 at 19:59
    
This answer will not save stderr. Use &> , see stackoverflow.com/questions/637827/… and tldp.org/LDP/abs/html/io-redirection.html – Panther Feb 14 '14 at 20:16
3  
The OP never asked to save stderr – chaos Feb 14 '14 at 20:18

An enhancement to consider -

Various scripts will inject color codes into the output which you may not want cluttering up your log file.

To fix this, you can use the program sed to strip out those codes. Example:

command 2>&1 | sed -r 's/'$(echo -e "\033")'\[[0-9]{1,2}(;([0-9]{1,2})?)?[mK]//g' | tee ~/outputfile.txt
share|improve this answer
    
How to save the output in a way that colours are conserved ? I would like to import the result of a command in libreoffice and keep the colours. – madrang May 12 '15 at 6:36
    
@madrang: I only read your comment now but you may find this answer useful. – Sylvain Pineau Sep 21 '15 at 10:41
    
Oh, almost exactly what I am looking for. How to print also on screen the output? – Sigur Dec 23 '16 at 22:10
    
Note that many commands that produce colorized output, such as ls and grep, support --color=auto, which outputs color codes only if standard output is a terminal. – Eliah Kagan Sep 3 '17 at 14:37

protected by Community Mar 12 '15 at 6:57

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.