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 63 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 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
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… and – bodhi.zazen Feb 14 '14 at 20:16
The OP never asked to save stderr – chaos Feb 14 '14 at 20:18
xD, if that's the thing: script is also a software. You can use a pencil if you don't want to use software =) – chaos Feb 14 '14 at 21:14

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 just the same with less characters to type:

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

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

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.