1. Overview

Sometimes, we need to save our terminal activity on a Linux machine to replay or review it later. This includes the input commands we use and the output that we get from the terminal. A record of this activity can be beneficial if we want to document our steps or debug something later.

In this tutorial, we’ll cover how we can save an interactive terminal session to a file using the script command.

2. Recording a Terminal Session With the script Command

The script command is a utility that records and saves the activity we’re performing in a terminal session. It stores all the input and output happening on the terminal to a target file which we can view or replay later.

Unlike the redirect operators, which require using redirection manually for each command and might not work in all command outputs, the script command automatically captures the interactive session while we’re using the terminal and can record any type of characters, file edits, and complex outputs.

The script command is part of the util-linux package, which is included by default in most Linux distributions.

To start recording the terminal session, we simply type script followed by the output file name:

$ script output.txt
Script started, output log file is 'output.txt'.

Here, we want to save the recorded session output to a file named output.txt. Now, let’s start typing some commands in the terminal:

$ pwd
/root
$ ls -ltrh
total 0
-rw-r--r-- 1 root root 0 Jan 31 05:11 output.txt
$ touch mynewfile.txt
$ ls
mynewfile.txt  output.txt

To end the session recording, we simply type exit:

$ exit
exit
Script done.

Now, let’s check our output.txt file using cat:

$ cat output.txt 
Script started on 2024-01-31 05:11:38+00:00
$ pwd
/root
$ ls -ltrh
total 0
-rw-r--r-- 1 root root 0 Jan 31 05:11 output.txt
$ touch mynewfile.txt
$ ls
mynewfile.txt  output.txt
$ exit
exit
Script done on 2024-01-31 05:18:07+00:00

Here, we can see that every input and output activity we’ve made in the terminal session is recorded in the file.

3. Using the -q Option for Quiet Mode

When using the script command, we can remove the first and last lines that appear at the start and end of the recording from the terminal output. To do this, we simply use the quiet mode by adding the -q option to the command:

$ script -q output.txt
$ whoami
root
$ exit
exit

Above, the command output didn’t show the Script started or Script done text on the terminal. However, if we check the contents of the output.txt file, they’ll still be there:

$ cat output.txt 
Script started on 2024-02-03 12:13:01+00:00
$ whoami
root
$ exit
exit
Script done on 2024-02-03 12:13:17+00:00 

We can see that quiet mode disables the starting and ending statements of the output only from the terminal, but not from the output file.

4. Creating a Timing File and Replaying a Terminal Session

Another popular usage for the script command is to create a timing file using the –t option. The timing file is used as a log that records timing information about the commands used in the session. This is useful when replaying the session in an interactive mode.

Now, let’s check how this works:

$ script --t=timing.txt -q output.txt
$ whoami
root
$ date
Sat Feb  3 01:33:40 PM UTC 2024
$ pwd
/root
$ ls
output.txt  timing.txt
$ exit
exit

Above, we used the script command with the –t option to create a timing file with the name timing.txt, and then, we issued some commands to record them in the session.

Now, let’s check the contents of the timing.txt file:

$ cat timing.txt 
0.017008 78
4.423770 1
0.082596 1
0.038649 1
0.752733 1
0.184588 1
0.684832 1
2.459559 11
0.004751 6
0.006062 78
------- OUTPUT TRIMMED --------

Here, we can see the timing information divided into two columns. The first column indicates the time elapsed since the previous output, and the second indicates how many characters were output this time.

Using this information along with the script output file, we can replay the session the same as if we’re watching the commands in a video, including the typing speed, misspelled and changed commands, and backspacing.

To replay the terminal session, we use the command scriptreplay and provide both the timing and output files:

$ scriptreplay --timing=timing.txt output.txt 
$ whoami
root
$ date
Sat Feb  3 01:33:40 PM UTC 2024
$ pwd
/root
$ ls
output.txt  timing.txt
$ exit
exit

Here, we provided the timing file using the –timing option followed by the output file name, which is output.txt. So, the scriptreplay command replayed the recorded session from the terminal.

5. Conclusion

In this article, we’ve covered the basics of how to use the script command to record an interactive terminal session. The script command saves all the output from the session into an output file that we can use to document our steps or troubleshoot issues later.

When combined with a timing file, the script command also enables replaying the terminal session as if it were in real time. To create a timing file for replaying the session, we provide the –t option to the script command. We can then use the scriptreplay command and provide both the timing and output files to start the session replay.

1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.