Recording the terminal in Linux with script

I have been looking for a standard way to record my terminal sessions in Linux recently, so did what most folks of the common era do, and searched for it.

I use both Fedora (at home) and Ubuntu (at work), so I was hoping for:

  • something in the standard package directories of both these distributions (dnf and apt, rspectively).

  • something I can run on the command-line and save as local files for replay.

  • something I can rely on to do the job right.

My search yielded a number of contenders:

  • script - a commonly-used tty recorder that came pre-installed on both Fedora 29 and Ubuntu 16.

  • ttyrec – claims to be a derivative of script. This wasn't in the package repository of either Fedora 29 or Ubuntu 16.

  • asciinema – a newer terminal recorder that features a web-service backend (so you can play your recordings on the web without converting them to video/gif first). This was in the Fedora 29 package repository (sudo dnf install asciinema), but did not exist in Ubuntu 16's repos.

Of the three, asciinema seems the slickest, has a website that a graphic designer put some effort upon, and allows you to upload your terminal sessions for playback. I may get back to that one in the future, as well as ttyrec, which probably does a nice job of terminal recording.

The script command

The script command was installed on both my target platforms, so installation was a non-issue.

Recording

To record a terminal session, run:

script -ttiming_file script_file

There are some guides out there that claim that the -t argument is optional, but I haven't found this to be the case (replaying requires it!). Also be aware that there's no space between the -t argument and the timing file name.

the recording will continue until you type Ctrl+d or:

exit

At which point, the file will be closed and you can either overwrite it (by recording using the same names), append to it or replay it.

Appending

You can append to an existing file by using the same command that you did to record it and adding the -a flag:

script -a -ttiming_file script_file

Replaying

To replay your script file, run:

scriptreplay timing_file script_file

The scriptreplay command requires the timing file as its first argument. This command will replay the script you recorded in the current terminal.