SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / ubuntu / Questions / 420981
Accepted
led-Zepp
led-Zepp
Asked: 2014-02-15 11:49:07 +0800 CST2014-02-15 11:49:07 +0800 CST 2014-02-15 11:49:07 +0800 CST

How do I save terminal output to a file?

  • 772

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.

command-line
  • 10 10 Answers
  • 3254225 Views

10 Answers

  • Voted
  1. Best Answer
    Seth
    2014-02-15T11:52:13+08:002014-02-15T11:52:13+08:00

    Yes it is possible, just redirect the output (AKA stdout) 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)

    • 1531
  2. Byte Commander
    2016-02-09T06:52:34+08:002016-02-09T06:52:34+08:00

    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.

    • 1443
  3. Aaron
    2014-06-20T20:45:53+08:002014-06-20T20:45:53+08:00

    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.

    • 132
  4. chaos
    2014-02-15T11:52:09+08:002014-02-15T11:52:09+08:00

    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
    
    • 23
  5. Sean Huber
    2014-07-09T12:57:02+08:002014-07-09T12:57:02+08:00

    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
    
    • 22
  6. WinEunuuchs2Unix
    2019-12-27T17:38:52+08:002019-12-27T17:38:52+08:00

    The script command

    There are two different questions here. The first is in the title:

    How do I save terminal output to a file?

    The second question is in the body:

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

    All the answers posted here address the second question but none address the first question which has a great answer in Unix & Linux:

    • Save all the terminal output to a file

    This answer uses a little known command called script which saves all your shell's output to a text file until you type exit. The command output still appears on your screen but also appears in the text file.

    The process is simple. Use:

    $ script ~/outputfile.txt
    Script started, file is /home/rick/outputfile.txt
    $ command1
    $ command2
    $ command3
    $ exit
    exit
    Script done, file is /home/rick/outputfile.txt
    

    Then look at your recorded output of commands 1, 2 & 3 with:

    cat ~/outputfile.txt
    

    This is similar to earlier answer of:

    command |& tee ~/outputfile.txt
    
    • But you don't have to use |& tee ~/outputfile.txt after each commnd.
    • The script command has added benefit (or disadvantage) of reloading ~/.bashrc when it starts.
    • The script command shows the command prompt ($PS1) followed by the command(s) you entered.
    • The script command records all the details in full color.

    Send output to clipboard

    Many times we want the output to go to the clipboard so we can paste it later. From this answer you can use:

    cat ~/.bashrc | xclip -selection clipboard
    

    Now you can use Ctrl+V in almost any application to paste the terminal output into your document. To paste the terminal output in the clipboard back into your terminal use Ctrl+Shift+V instead.

    • 18
  7. Franck Dernoncourt
    2018-07-05T12:54:00+08:002018-07-05T12:54:00+08:00

    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?

    • 13
  8. tripleee
    2018-04-12T04:25:38+08:002018-04-12T04:25:38+08:00

    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.

    • 11
  9. Bersan
    2021-10-05T04:59:33+08:002021-10-05T04:59:33+08:00

    If you want to output to the file while the command is being run:

    script -c ./path/to/executable.bash -f log.txt
    
    • 3
  10. pbhj
    2022-01-14T16:02:39+08:002022-01-14T16:02:39+08:00

    Use terminal emulator features

    An option not mentioned yet, that can save colours / colors too, is to use a console program — such as Konsole (KDE/Plasma's default terminal emulator) — to save the output.

    Konsole

    Konsole has: 
    File > Save output as... 
    

    the shortcut is Ctrl+Shift+S; it allows the output to be saved as a text file, or as HTML including colors! I'm not sure exactly how much it will save but in my test it only included ~1000, the entire terminal scrollback, (you can increase the buffer by creating a new profile, Profile > New..., and then change the Scrolling settings to capture more; Konsole version 4:21.08.1).

    gnome-terminal

    gnome-terminal has "copy output as HTML" too, which allows pasting the HTML into a document; it preserves colour but only copies the content of the output currently shown on screen AFAICT.

    generically

    You can, of course, do a straight drag-select (hold left mouse button whilst dragging) and then copy (ctrl+c, Edit > Copy, or right-mouse-click and choose copy).

    others?

    Feel free to modify this answer to include other popular terminal apps. My favourite, Yakuake, does not appear to have this feature nor did most of the popular terminals I reviewed, including terminal.app and Hyper.

    • 1

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    How to install Google Chrome

    • 8 Answers
  • Marko Smith

    Is there a command to list all users? Also to add, delete, modify users, in the terminal?

    • 9 Answers
  • Marko Smith

    How to delete a non-empty directory in Terminal?

    • 4 Answers
  • Marko Smith

    How to unzip a zip file from the Terminal?

    • 9 Answers
  • Marko Smith

    How can I copy the contents of a folder to another folder in a different directory using terminal?

    • 8 Answers
  • Marko Smith

    How do I install a .deb file via the command line?

    • 11 Answers
  • Marko Smith

    How do I run .sh scripts?

    • 16 Answers
  • Marko Smith

    How do I install a .tar.gz (or .tar.bz2) file?

    • 14 Answers
  • Marko Smith

    How to list all installed packages

    • 24 Answers
  • Marko Smith

    Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?

    • 25 Answers
  • Martin Hope
    Flimm How can I use docker without sudo? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    led-Zepp How do I save terminal output to a file? 2014-02-15 11:49:07 +0800 CST
  • Martin Hope
    ubuntu-nerd How to unzip a zip file from the Terminal? 2011-12-11 20:37:54 +0800 CST
  • Martin Hope
    pandisvezia How can I copy the contents of a folder to another folder in a different directory using terminal? 2011-12-11 17:19:37 +0800 CST
  • Martin Hope
    TheXed How do I install a .deb file via the command line? 2011-05-07 09:40:28 +0800 CST
  • Martin Hope
    Ivan How to list all installed packages 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra Unable to lock the administration directory (/var/lib/dpkg/) is another process using it? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry How do I determine the total size of a directory (folder) from the command line? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher "The following packages have been kept back:" Why and how do I solve it? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford How can PPAs be removed? 2010-07-30 01:09:42 +0800 CST

Related Questions

Trending Tags

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve