Save all the terminal output to a file

  • Is there some way of saving all the terminal output to a file with a command?

    • I'm not talking about redirection command > file.txt
    • Not the history history > file.txt, I need the full terminal text
    • Not with hotkeys !

    Something like terminal_text > file.txt

    Many GUI terminal emulators allow to save the scroll buffer, but this is not accessible to commands (leaving aside `xdotool` and this sort of black art).

    Try Terminal menu -> Shell -> Export text as, like here:

  • heemayl

    heemayl Correct answer

    6 years ago

    You can use script. It will basically save everything printed on the terminal in that script session.

    From man script:

    script makes a typescript of everything printed on your terminal. 
    It is useful for students who need a hardcopy record of an 
    interactive session as proof of an assignment, as the typescript file 
    can be printed out later with lpr(1).

    You can start a script session by just typing script in the terminal, all the subsequent commands and their outputs will all be saved in a file named typescript in the current directory. You can save the result to a different file too by just starting script like:

    script output.txt

    To logout of the screen session (stop saving the contents), just type exit.

    Here is an example:

    $ script output.txt
    Script started, file is output.txt
    $ ls
    output.txt  testfile.txt  foo.txt
    $ exit
    Script done, file is output.txt

    Now if I read the file:

    $ cat output.txt
    Script started on Mon 20 Apr 2015 08:00:14 AM BDT
    $ ls
    output.txt  testfile.txt  foo.txt
    $ exit
    Script done on Mon 20 Apr 2015 08:00:21 AM BDT

    script also has many options e.g. running quietly -q (--quiet) without showing/saving program messages, it can also run a specific command -c (--command) rather than a session, it also has many other options. Check man script to get more ideas.

    Can it be invoked after the fact? (i.e. At the end of a session) Or does it have to be invoked before the content you want logged?

    @tjt263 It has to be invoked before the contents you want to be saved..

    Damn. That's a shame. I don't usually know I want it until afterwards.

    @tjt263 you could put it inside your .bashrc and append everything into /tmp.

    To export it retroactively, try Terminal menu -> Shell -> Export text as, like here:

    Thanks. This saved my day. But I had to use `script -c "rails runner -e development lib/scripts/my_script.rb" report.txt` because just using `script` then ran the `rails runner` in script session didn't wrote everything in the output file. Reference:

    @Magne you should note in your comment that it is specific to the terminal utility in Mac. gnome-terminal, for one, does not have that menu, so we should assume that other linux terminal windows won't have it either.

    @maxweel thanks for pointing that out. I cannot edit my comment now, but I think your comment serves as that note now.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM

Tags used