Home Command line utilities How To Record Everything You Do In Terminal
Record Everything You Do In Terminal Using Script Command

How To Record Everything You Do In Terminal

By sk
1262 Views

We already published a guide that explained how to save commands in terminal itself and use them on demand. It is very useful for those who don't want to memorize a lengthy and complex Linux commands. Today, in this guide, we are going to see how to record everything you do in Terminal using script command. You might have run a command, or created a directory, or installed an application in Terminal. Script command simply saves whatever you did in the Terminal. You can then view them if you want to know what you've done a few hours or days ago. I know I know, we can use UP/DOWN arrow keys or history command to view previously running commands. However, you can't view the output of those commands. But, Script command records and displays complete terminal session activities.

The script command creates a typescript of everything you do in the Terminal. It doesn't matter whether you install an application, create a directory/file, remove a folder. Everything will be recorded, including the commands and the respective outputs. This command will be helpful who wants a hard-copy record of an interactive session as proof of an assignment. Whether you're a student or a tutor, you can make a copy of everything you do in the Terminal along with all outputs.

Record Everything You Do In Terminal Using Script Command In Linux

The script command is part of the util-linux package on RPM-based systems and bsdutils package on DEB-based systems and it comes pre-installed on most modern Linux operating systems. So, let us not bother about the installation.

Let us go ahead and see how to use it in real time.

Run the following command to start the Terminal session recording.

$ script -a my_terminal_activities

Where, -a flag is used to  append the output to file or to typescript, retaining the prior contents. The above command records everything you do in the Terminal and append the output to a file called 'my_terminal_activities' and save it in your current working directory.

Sample output would be:

Script started, file is my_terminal_activities

Now, run some random Linux commands in your Terminal.

$ mkdir ostechnix
$ cd ostechnix/
$ touch hello_world.txt
$ cd ..
$ uname -r

After running all commands, end the 'script' command's session using command:

$ exit

After typing exit, you will the following output.

exit
Script done, file is my_terminal_activities

Record Everything You Do In Terminal Using Script Command In Linux

As you see, the Terminal activities have been stored in a file called 'my_terminal_activities' and saves it in the current working directory.

You can also save the Terminal activities in a file in different location like below.

$ script -a /home/ostechnix/documents/myscripts.txt

All commands will be stored in /home/ostechnix/documents/myscripts.txt file.

View Recorded Terminal Activities

To view your Terminal activities, just open this file in any text editor or simply display it using the 'cat' command.

$ cat my_terminal_activities

Sample output:

Script started on 2019-10-22 12:07:37+0530
[email protected]:~$ mkdir ostechnix
[email protected]:~$ cd ostechnix/
[email protected]:~/ostechnix$ touch hello_world.txt
[email protected]:~/ostechnix$ cd ..
[email protected]:~$ uname -r
5.0.0-31-generic
[email protected]:~$ exit
exit

Script done on 2019-10-22 12:08:10+0530

View Terminal Activities

As you see in the above output, script command has recorded all my Terminal activities, including the start and end time of the script command. Awesome, isn't it? The reason to use script command is it's not just records the commands, but also the commands' output as well. To put this simply, Script command will record everything you do on the Terminal.

Play The Recorded Terminal Sessions

Sometimes you might want to replay the Terminal sessions, like an animated GIF, instead of just viewing it. If so, you can easily do it with Scriptreplay command. Refer the following guide to know how to play the recorded Terminal sessions.

Record a single command

It is also possible to record the output of a single command using -c flag like below.

$ script -c "lsb_release -a" -a my_terminal_activities

Sample output:

Script started, file is my_terminal_activities
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.3 LTS
Release: 18.04
Codename: bionic
Script done, file is my_terminal_activities

To view the recorded command's output, simply run:

$ cat my_terminal_activities

It is quite useful for showing the demonstration of a specific command's usage to the students or share the output of a command to your colleagues or support team.

Record Terminal sessions with timestamps

As one of our reader Mr.Alastair Montgomery mentioned in the comment section below, we could setup an alias which would timestamp the recorded sessions.

Create an alias for the script command like below.

$ alias rec=’script -aq ~/term.log-$(date “+%Y%m%d-%H-%M”)’

Now simply enter the following command start recording the Terminal.

$ rec

Now, all your Terminal activities will be logged in a text file with timestamp, for example term.log-20191022-12-16.

Record Terminal activities with timestamps


Suggested read:


Record Terminal sessions in quiet mode

As you noticed, when we start the script command to record the terminal session it displays a welcome message - "Script started, file is my_terminal_activities" and displays "Script done, file is my_terminal_activities" after finished the recording. If you don't want to view those messages, you can run script command in quiet mode using -q flag like below.

$ script -a -q my_terminal_activities

For more details, refer man page.

$ man script

Conclusion

Like I said, script command would be useful for students, teachers and any Linux users who wants to keep the record of their Terminal activities. Even though there are many CLI and GUI tools available to do this, script command is an easiest and quickest way to record the Terminal session activities.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

6 comments

Alastair Montgomery March 13, 2017 - 2:10 pm

You could setup an alias with would timestamp your recorded sessions.

alias rec=’script -aq ~/term.log-$(date “+%Y%m%d-%H-%M”)’

Reply
SK March 13, 2017 - 8:59 pm

Thank you. I never know this trick. Much appreciated. I will update your points in the guide soon.

Reply
cmcanulty March 13, 2017 - 4:30 pm

is there a way to have it always saved to a certain location, say for example/home/user/documents/my scripts?

Reply
SK March 13, 2017 - 8:58 pm

Yes, just use “script -a /home/user/documents/myscripts”. All commands will be stored in the specified file.

Reply
Chris McGimpsey-Jones October 26, 2019 - 3:23 am

This is excellent. I’ve been searching for something just like this.

Reply
mehdi November 7, 2019 - 6:44 pm

thanks, this was very useful.
I wanna record my new users and when I add this to /etc/skel/.bashrc and when I adduser and try to login, it generates like 100 sessions.
even with my previous users, make 100 sessions.
I wonder if you guys could help me with this.
thanks.

Reply

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. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More