Some times its require to record your terminal sessions for future use or some sort of educational purpose. Recording terminal session can be achieved by installing script command(Most of the distributions install this application by default). If the command is not there try to install the package which provides it.

Install script command in Linux(Redhat/Fedora/CentOS) Operating systems

yum install util-linux-ng

Installing script command in Linux(Ubuntu/Debian) Operating systems

apt-get install bsdutils

Once you install the application you will get two commands one for recording the terminal session(The script command) and playing the recorded terminal session(The scriptreplay command).

Recording your terminal: To record your terminal you have to use script as shown below.  Once this command is executed you start using your terminal as you use normally.

script -t -a 2> /tmp/time.txt /tmp/record.txt

-t for mentioning the time so that separate time stamps are logged in to /tmp/time.txt file and -a for appending those time stamps to /tmp/time.txt file. If you see in the above command we used error redirect(2>), this is nothing but File descriptor to send time details to /tmp/time.txt. and the last file /tmp/record.txt records your keystrokes and their outputs.

How it works?

When you run the above command, it will say the script started. From this point the script command monitors the time and keeps track of the time changes. Onc user typed some words, it use the time difference between each keystroke user typed and store that time changes in the time file. The keys what he pressed is stored in /tmp/record.txt file. For example I clipped a time file for your reference.

Clipped time file:

0.362377 54
0.164000 1
2.008364 1
0.100571 1
0.013831 1
0.069171 2
1.316989 1
0.270694 1
0.114222 1
0.016042 1
0.043393 135

If you see its just have numbers nothing more than that.

How about recorded data file /tmp/record.txt content?

Script started on Thu 04 Apr 2013 11:18:21 AM IST
[email protected]:~$ asdf
asdfNo command ‘asdf’ found, did you mean:
Command ‘asdfg’ from package ‘aoeui’ (universe)
Command ‘sadf’ from package ‘sysstat’ (main)
Command ‘sdf’ from package ‘sdf’ (universe)
asdf: command not found
[email protected]:~$ asdf
No command ‘asdf’ found, did you mean:
Command ‘asdfg’ from package ‘aoeui’ (universe)
Command ‘sadf’ from package ‘sysstat’ (main)
Command ‘sdf’ from package ‘sdf’ (universe)
asdf: command not found
[email protected]:~$ exit

Script done on Thu 04 Apr 2013 11:18:30 AM IST

 

Once you start script command, it will start recording your session by displaying below line

[email protected]:~$ script -t -a 2> /tmp/time.txt /tmp/record.txt
Script started, file is /tmp/record.txt

Once you completed executing your command execution, you can exit the script command by type exit or pressing Ctrl+d

Example output

s[email protected]:~$ exit
exit
Script done, file is /tmp/record.txt
[email protected]:~$

If you see it say’s that the script file done writing data to the file. From that file you can view what are the commands you executed and their output by using cat command. But this will not give a simulated output. If you want simulated output you can use scriptreplay command as shown below.

scriptreplay /tmp/time.txt /tmp/record.txt

scriptreplay command takes two files as input, one for time stamps and other for recorded data file. Still have queries? See our youtube video how to do it yourself below. Please subscribe to our channel by clicking here.

Stay tuned to our other posts.