53

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

60

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
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
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 May 15 '16 at 16:02
  • @tjt263 It has to be invoked before the contents you want to be saved.. – heemayl May 15 '16 at 16:07
  • 4
    Damn. That's a shame. I don't usually know I want it until afterwards. – tjt263 May 15 '16 at 17:09
  • @tjt263 you could put it inside your .bashrc and append everything into /tmp. – Blauhirn Jul 5 '17 at 0:38
  • 1
    To export it retroactively, try Terminal menu -> Shell -> Export text as, like here: mactricksandtips.com/2013/04/save-terminals-text-output.html – Magne Sep 22 '17 at 11:23
11

I too faced the same problem and after some search came up with this solution:

Add to your .bash_aliases this:

# Execute "script" command just once
smart_script(){
    # if there's no SCRIPT_LOG_FILE exported yet
    if [ -z "$SCRIPT_LOG_FILE" ]; then
        # make folder paths
        logdirparent=~/Terminal_typescripts
        logdirraw=raw/$(date +%F)
        logdir=$logdirparent/$logdirraw
        logfile=$logdir/$(date +%F_%T).$$.rawlog

        # if no folder exist - make one
        if [ ! -d $logdir ]; then
            mkdir -p $logdir
        fi

        export SCRIPT_LOG_FILE=$logfile
        export SCRIPT_LOG_PARENT_FOLDER=$logdirparent

        # quiet output if no args are passed
        if [ ! -z "$1" ]; then
            script -f $logfile
        else
            script -f -q $logfile
        fi

        exit
    fi
}

# Start logging into new file
alias startnewlog='unset SCRIPT_LOG_FILE && smart_script -v'

# Manually saves current log file: $ savelog logname
savelog(){
    # make folder path
    manualdir=$SCRIPT_LOG_PARENT_FOLDER/manual
    # if no folder exists - make one
    if [ ! -d $manualdir ]; then
        mkdir -p $manualdir
    fi
    # make log name
    logname=${SCRIPT_LOG_FILE##*/}
    logname=${logname%.*}
    # add user logname if passed as argument
    if [ ! -z $1 ]; then
        logname=$logname'_'$1
    fi
    # make filepaths
    txtfile=$manualdir/$logname'.txt'
    rawfile=$manualdir/$logname'.rawlog'
    # make .rawlog readable and save it to .txt file
    cat $SCRIPT_LOG_FILE | perl -pe 's/\e([^\[\]]|\[.*?[a-zA-Z]|\].*?\a)//g' | col -b > $txtfile
    # copy corresponding .rawfile
    cp $SCRIPT_LOG_FILE $rawfile
    printf 'Saved logs:\n    '$txtfile'\n    '$rawfile'\n'
}

And to the end of your .bashrc file add this:

smart_script

After you've done this, "script" command will be executed once in every terminal session, logging everything to '~/Terminal_typescripts/raw'. If you want, you can save current session log after the fact (in the end of the session) by typing 'savelog' or 'savelog logname' - this will copy current raw log to '~/Terminal_typescripts/manual' and also create readable .txt log in this folder. (If you forget to do so, raw log files will still be in their folder, you'll just have to find them.) Also you may start recording to a new log file by typing 'startnewlog'.

There will be a lot of junk log files, but you can clean old ones from time to time, so it's not a big problem.

(Based on https://answers.launchpad.net/ubuntu/+source/gnome-terminal/+question/7131 , https://askubuntu.com/a/493326/473790 )

1

As already stated, you could use script if you know beforehand that you will want to save the text of the session. Also, copying the terminal output and pasting it into a text editor seems to do the trick I believe.

protected by Community Jan 12 '17 at 13:51

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.