Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 78 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
    
Note that someCommand 2> someFile.txt and someCommand 2>> someFile.txt also redirects stterr to someFile.txt – Slothworks Aug 29 '15 at 13:32

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

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

share|improve this answer
3  
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 at 13:51
    
@tim687 I have removed that edit. Sorry about that...wasn't a part of my original answer. – Aaron Apr 6 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 at 6:43

You can pipe the command output to a file:

your_command >/path/to/file

Or append the command output to a file

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 – bodhi.zazen 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

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

Overview:

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

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 only, 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.

share|improve this answer

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.

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.