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.

up vote 458 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 as well use this:

SomeCommand &> SomeFile.txt  

or this to append:

SomeCommand &>> SomeFile.txt  

if you want to have both stderr and output displayed on the console and in a file use this:

SomeCommand 2>&1 | tee SomeFile.txt

(If you want the output only, drop the 2 above)

  • 10
    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
  • @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
  • 1
    NB: to get the output of the make command into a file it requires this syntax instead: make > someFile.txt 2>&1 (source: linuxquestions.org/questions/linux-newbie-8/…) – Gabriel Staples Jan 15 at 0:49
  • 1
    @KyleBridenstine See the tee answer below (askubuntu.com/a/485762/44179) – Seth Jul 11 at 20:03

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.

  • 41
    Thanks for the table, it's excellent! This should be top answer – DevShark Aug 15 '16 at 16:24
  • 2
    @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
  • 13
    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
  • 2
    @ByteCommander I get the error: sh: 1: Syntax error: "&" unexpected when I use |& tee from a Python script in a c9.io server. It seems a different shell is being used. echo $SHELL shows /bin/bash and $SHELL --version shows version 4.3.11(1)-release. I tried #!/bin/bash in my python script but I still get sh: 1: Syntax error. I got what I needed so I'm giving up on sorting the weirdness between sh and bash on my server. Thanks. – samkhan13 Jan 28 at 3:09
  • 1
    @samkhan13 looks like you are running sh and not bash (or maybe bash in sh mode...). You can check what exactly your current shell process is using ps -p $$ -o cmd=, because echo $SHELL is unreliable and will show you your login shell, ignoring whether you might have started a different subshell. – Byte Commander Jan 28 at 12:35

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.

  • 11
    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
  • how do I interpret meaning of 2>&1? – Mahesha999 Jul 15 '16 at 7:47

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

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

For cron jobs etc you want to avoid the Bash extensions. The equivalent POSIX sh redirection operators are

Bash            POSIX
--------------  --------------
foo &> bar      foo >bar 2>&1
foo &>> bar     foo >>bar 2>&1
foo |& bar      foo 2>&1 | bar

You'll notice that the POSIX facility is in some sense simpler and more straightforward. The &> syntax was borrowed from csh which should already convince you that it's a bad idea.

some_command | tee command.log and some_command > command.log have the issue that they do not save the command output to the command.log file in real-time.

To avoid that issue and save the command output in real-time, you may append unbuffer, which comes with the expect package.


Example:

sudo apt-get install expect
unbuffer some_command | tee command.log
unbuffer some_command > command.log

Assuming log.py contains:

import time
print('testing')
time.sleep(100) # sleeping for 100 seconds

you can run unbuffer python log.py | tee command.log or unbuffer python log.py > command.log

More information: How can I save a command output to a file in real-time?

  • They do save the output as they receive it, the problem is that python turns on buffering when the output is not to a TTY. Other options for disabling this in Python: stackoverflow.com/q/107705/2072269 – muru Jul 5 at 2:22

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.