Home Linux Tips & Tricks How To Record Your Terminal Activity Using Script Command In Linux

How To Record Your Terminal Activity Using Script Command In Linux

By sk
Published: Last Updated on 1.3K views

As a System administrator, you will run a lot of commands in the Terminal everyday. Sometimes you might want to refer the entire command history along with all respective outputs. And, If you're a programmer and write a program that displays a really long output in Terminal, you can't scroll up to certain limit and can't view the entire output of your Terminal session. If you're a technical writer, you must include what command you used in the Terminal and it's output in your guides. So, I believe It is always a best idea to record the Terminal session, and keep it aside for future reference. There are many tools out there to record your Desktop. Unfortunately, there are no such tools for servers that only has CLI session. Luckily, we have a simple command called script that allows us to make typescript of everything printed on the Terminal.

Script command allows you to record everything you do in your Terminal, and saves the output in a text file. This command comes pre-installed with most Linux, and Unix-like operating systems. In this brief tutorial, let me show you how to use script command to record your Terminal session.

How to Use Script Command to Record Linux Terminal Activities

When you're ready to recording the Terminal activity, just type:

$ script

You will get a message something like below.

Script started, file is typescript
Use Script Command to Record Linux Terminal Activities

Use Script Command to Record Linux Terminal Activities

Now, everything you entered in the Terminal will be saved in a file called typescript.

Also, you can give a custom name to the typescript by specifying a file name of your choice as shown below.

$ script -a my_terminal_session

Now, Let us type few commands, and see how it works.

$ whoami
$ uname -a
$ cd /home/sk/Soft_Backup
$ ls -l
$ mkdir ostechnix
$ rmdir ostechnix

That's enough for now. You can try as many commands as you want to record. Once you are done, type 'exit' in the Terminal to stop recording.

$ exit

Sample output:

Script done, file is typescript

As you see in the above screenshot, the script command's output will be stored in file called "typescript" in the current working directory.

Now, let us go ahead, and check what we did so far in the Terminal.

View Script Command Output

$ cat typescript

Sample output:

Script started on Friday 18 March 2016 01:29:06 PM IST
sk@sk:~$ whoami
sk@sk:~$ uname -a
Linux sk 4.4.5-040405-generic #201603091931 SMP Thu Mar 10 00:34:17 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
sk@sk:~$ cd /home/sk/Soft_Backup
sk@sk:~/Soft_Backup$ ls -l
total 16
drwxrwxr-x 2 sk sk 4096 Nov 12 2014 Linux Soft
drwxrwxr-x 5 sk sk 4096 May 30 2015 OS Images
drwxrwxr-x 30 sk sk 4096 Mar 11 17:46 VHD's
drwxrwxr-x 17 sk sk 4096 Dec 30 11:48 Windows Soft
sk@sk:~/Soft_Backup$ mkdir ostechnix
sk@sk:~/Soft_Backup$ rmdir ostechnix
sk@sk:~/Soft_Backup$ exit

Script done on Friday 18 March 2016 01:29:44 PM IST
View Script Command Output

View Script Command Output

Voila! As you see in the above output, Script command recorded and displayed everything I have entered in the Terminal. For the sack of easy reference, I have marked the commands that I executed in bold letters.

You could use the output for your assignment, or just save this output for future reference.

For further details, I recommend you to refer the man pages.

$ man script

That's all for now. If you want a hard-copy record of the Terminal session for future reference, or for an assignment, script command is good tool to try.

You May Also Like


Dan Stromberg March 19, 2016 - 8:34 am

I rewrote script in Python: http://stromberg.dnsalias.org/~strombrg/pypty/ . Its chief benefit is that it has a “dated files” mode, so if you leave a session running overnight, a new file will be started for the new day. This makes it easier to look things up later. The distribution also includes script-replay, which makes it easy to move forward and back through a typescript; it has a curses interface (arrow keys) and a GTK (buttons) interface.

SK March 19, 2016 - 11:26 am

Hello Dan, I visited the link you have mentioned. Your script seems good and might be useful for users. If you have time, please write a review or how-to guide about your script with few examples and send it to me by using the contact form. If it is good, I will publish on our blog, and let us promote your script. Thank you for letting me about your script. https://ostechnix.com/contact-us/


Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More