How to use the Script command to record terminal sessions in Linux?

Script

The script command in Linux makes a typescript of everything on your terminal session. It stores the information in raw form to a log file. Additionally, it also gives you the option to store information about the timing of commands. This is useful in case you want to see the realtime replay of your session. We will cover this in the later section of the tutorial.

The script command is very useful for system administration. It enables admins to look for errors by going through the session history in real-time. In this tutorial we will look at how to use the script command to record and log your terminal sessions.

Let’s get started.

How to use the Script command to log your terminal sessions?

To log your terminal sessions using the script command you need to use the following syntax :

script [filename] 

Everything you enter after this command will be recorded to the file you mentioned along with the script command.

To stop the recording use :

exit 

Let’s see an example :

Script
Script

This creates a typescript of your current terminal session under the filename ‘log‘.

View the typescript generated by the script command

After creating the typescript of the terminal session as shown above, let’s see how to view this file.

To view the file, you can use the cat command:

cat log 
Log
Log

How to append to an existing typescript?

You can record a new session and append it to an existing file instead of generating new files To append to an existing log file use the -a flag:

 script -a log

Let’s see an example. Here we are going to append to the file from our pervious example.

Append
Append

View the file using :

cat log 

Output :

Log File After Appending
Log File After Appending

How to replay the terminal session in real-time?

To replay the terminal session, you need to record the information about the time along with the commands. To record this time log, use the -timing flag along with script command.

The syntax for that is :

script --timing=[filename1] [filename2] 

Here, filename1 will contain information about time and filename2 will contain the typescript of the terminal session.

Let’s see an example.

Realtime
Realtime

Here, cmd_time is the file that contains the time information and realtime is the typescript of the terminal session.

We can see the contents of cmd_time using the cat command.

cat cmd_time 

Output :

Cmd Time
Cmd Time

You can view the typescript using :

cat realtime 

Output :

Typescript
Typescript

To replay the terminal session we will use the scriptreplay command. Scriptreplay command can playback typescripts using time information.

The syntax for that is :

scriptreplay [filename1] [filename2] 

Here filename1 is the file with information about time and filename2 is the typescript.

Script-replay of the example above is shown below.

Conclusion

This tutorial was about script command in Linux. We learnt how to use the script command for creating typescripts of our terminal sessions. We also leant how to replay a terminal session by storing information about time.