How to Record and Replay Linux Terminal Sessions

In this guide, we are going to look at how to use a script and scriptreplay commands in Linux that can help you to record commands and their output printed on your terminal during a given session.

The history command is a great command-line utility that helps users to store previous commands used, though it does not store the output of a command.

Therefore the script command comes in handy to provide you a powerful functionality that helps you to record everything that is printed on your terminal to a log_file.

You can then refer to this file later on in case you want to view the output of a command in history from the log_file.

You can also replay commands that you recorded using the scriptreplay command by using timing information.

How to Record a Terminal in Linux?

If you want to create a typescript (record of a terminal session) in Linux, you can use the script command which starts a new shell session and records everything displayed on the terminal, including input and output, to the specified filename.

script my_terminal_session.txt

After running the script command, you will see the terminal prompt, where you can run various commands and your terminal session will be recorded to the “my_terminal_session.txt” file.

Now try to execute a few commands to allow the script to record executed commands on the terminal.

cal
w
uptime
whoami
pwd

To stop recording, simply type exit or press Ctrl+D.

exit
Record Linux Terminal
Record Linux Terminal

Now try to view the log file ‘my_terminal_session.txt‘ for all recorded commands, while you view the log you realize that the script also stores line feeds and backspaces.

vi my_terminal_session.txt
View Linux Terminal Recording
View Linux Terminal Recording

You may use the -a option to append the log file or typescript, retaining the prior contents.

script -a my_terminal_session.txt
vi my_terminal_session.txt

To log the results of a single command other than an interactive shell session, use the -c option.

script -c 'hostname' script.log

If you want the script to run in a quiet mode then you can use the -q option. You will not see a message that shows the script is starting or exiting.

script -c 'who'  -q  script.log

To set timing information to standard error or a file use the --timing option. The timing information is useful when you want to re-display the output stored in the log_file.

Let us start the script and run the following commands w, uptime, and cal to be recorded.

script --timing=time.txt script.log

You can view the script.log and time.txt file for the timing command above.

vi script.log

Now view the time.txt file.

vi time.txt

The time.txt file has two columns, the first column shows how much time has elapsed since the last display and the second column, shows the number of characters that have been displayed this time around.

Use the man page and --help to seek more options and help in using the script command-line utility.

How to Replay Recorded Terminal Session in Linux?

The scriptreplay command helps to replay information in your log_file recorded by the script command.

The timing information is defined by the -timing=file option used with the script command and the file in this case is script.log which was used with the script command.

scriptreplay --timing=time.txt script.log
Replay Last Executed Commands in Linux
Replay Last Executed Commands in Linux

When the log_file is replayed using the timing information, the commands recorded are run and their output is displayed at the same time the original output was displayed while being recorded.

Summary

These two commands, script and scriptreplay easy to use and help a lot when you need to run the same batch of commands several times.

They help a lot in managing servers that have only a command-line interface for interaction with your system. Hope this guide was useful and if you have anything to add or face a challenge while using them, do not hesitate to post a comment.

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

31 thoughts on “How to Record and Replay Linux Terminal Sessions”

  1. Hello

    Is there any way to eliminate the ^m and other special characters from the output file? I think those characters are related to the screen color which gets appended before each character with a different color.

    Reply
  2. For those trying to get this working on macOS 10.13.x I was able to do,

    # script -r /path/to/my/fancy-42-recording.log
    

    and then to playback the recording with `script`

    # script -p /path/to/my/fancy-42-recording.log
    
    Reply
  3. hello, cool article :)

    i find a funky typo -> You will not see a massage that shows script is starting or exiting.
    you write “massage” instead of “message”

    peace

    Reply
  4. How do I append a session of terminal & do scriptreplay with the append session, I am trying like this:

    # script --append --timing=1.timing 1.session
    # scriptreplay 1.timing 1.session
    

    When I ran this first time and save the session I can see the replay through:

    # scriptreplay 1.timing 1.session
    

    but when I want to add or append more session to the same script then I am not able to run properly the same script with append one through the same command

    Please help me out!!

    Reply
    • @Shashank

      Try to specify the log file in front of the -a flaf like this:
      script –timing=1.timing -a 1.session

      Reply
    • @Dipak

      Simply start script command and run function as any other command, it will save the output to the script command file as explained in the article.

      Reply
  5. Hi Aaron,

    I’m unable to direct the timing information in a file.

    script --timing=filename.txt script.log
    script: invalid option — ‘-‘
    usage: script [-a] [-f] [-q] [-t] [file]

    Could you please help out.

    Reply
  6. Hi Aaron,

    I’m unable to get the time information directed to the time file.

    [ ~]#script –timing=filename.txt script.log
    script: invalid option — ‘-‘
    usage: script [-a] [-f] [-q] [-t] [file]
    #script –version
    script (util-linux-ng 2.17.2)

    Could you please help me out. I’m using centOS 6.

    Reply
  7. Getting this error, any idea? It dies immediately.

    $ script
    Script started, file is typescript
    wsh: invalid option — ‘i’
    error=3 Invalid parameter
    usage: wsh [-d][ -c ][ -p ][ -s ]
    Sets login environment and spawns an interactive server.
    -d enable debugging
    -c set a single executed command
    -p set the configuration full path
    -s set the shell full path
    Script done, file is typescript

    Reply
    • This could be a possible cause of the error:

      I have looked around for info about the error, according to what i found, wsh from: https://github.com/chenyf/wsh is used to execute command in a Linux container through unix socket.

      There could be certain configuration issues to do with wsh on your machine causing the error.

      Reply
  8. I wrote pypty, which is a pure-python reimplementation of script(1). It includes script-replay (note the dash), which allows you to step through your scripts with arrow keys (curses) or buttons (GObject Introspection).

    Reply
    • @kunalsing You need to use the – -timing=filename.txt option with script first to create the timing information file, you can use any other name apart from time.txt, then use
      – -timing option with scriptreplay ie – -timing=filename.txt . The filename used with script must be the same as the one used with scriptreplay.

      Reply
  9. I find these kind of tools very useful for tutorial purposes.
    Next to script there’s also ttyrec (available thru package manager on almost any distro).

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.