Script – A Simple Command To Record Your Terminal Session Activity
By default everyone prefer history command to review/recall the previously entered commands in terminal but unfortunately, that shows only the commands and doesn’t shows the commands output which was performed previously.
There are many utilities available in GUI to record the terminal session activity but very few applications are available for CLI and script is one of the best utility to record your terminal session on headless server.
It’s a best practices to record your terminal activity and keep it safe for further reference, even if you want to know the output of your command later sometime to investigate on this, it might help you.
Script is a Unix command line utility that records a terminal session (in other terms, It’s record everything displayed on your terminal). It stores the output in the current directory as a text file and the default file name is typescript.
Script is one of the Linux core utility and by default pre-installed in most of the Linux distributions. The script command is part of the util-linux-ng package.
How To Check Whether Scrip Command Installed Or Not?
The script command is part of the util-linux-ng package, so grep this package to confirm whether the script command installed or not in system.
For RPM based systems
$ yum list installed | grep util-linux-ng util-linux-ng.x86_64 2.17.2-12.14.el6 @base
For DEB based systems
$ apt-cache policy util-linux util-linux: Installed: 2.28.2-1ubuntu1.1 Candidate: 2.28.2-1ubuntu1.2 Version table: 2.28.2-1ubuntu1.2 500 500 http://in.archive.ubuntu.com/ubuntu yakkety-updates/main amd64 Packages *** 2.28.2-1ubuntu1.1 100 100 /var/lib/dpkg/status 2.28.2-1ubuntu1 500 500 http://in.archive.ubuntu.com/ubuntu yakkety/main amd64 Packages
How To Use Script Command
script command on terminal before start working, instantly it will start record the current terminal activity, once you have done the work, finally type
exit to complete the current recording.
To initiate the recording, just type
script command on terminal and you will get a message like below.
$ script Script started, file is typescript
Let’s run few commands on terminal to check this experiment.
$ lsb_release -a
$ uname -a
$ free -m
Finally exit the script by inputting the
exit command on terminal.
$ exit exit Script done, file is typescript
Check Script Command Output
We have successfully exit the script command, after executing few commands for this experiment. Now, we are going to print the script command output (The default output file name is typescript) with help of cat command.
$ cat typescript Script started on Tuesday 04 July 2017 08:37:34 AM IST [email protected]:~$ w 08:37:45 up 1 day, 48 min, 1 user, load average: 0.51, 0.27, 0.22 USER TTY FROM [email protected] IDLE JCPU PCPU WHAT magi tty7 :0 Mon07 24:48m 21:15 17.25s /sbin/upstart --user [email protected]:~$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.10 Release: 16.10 Codename: yakkety [email protected]:~$ last wtmp begins Mon Jul 3 07:54:43 2017 [email protected]:~$ uname -a Linux daygeek 4.10.1-041001-generic #201702260735 SMP Sun Feb 26 12:36:48 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux [email protected]:~$ pwd /home/magi [email protected]:~$ df -h Filesystem Size Used Avail Use% Mounted on udev 979M 0 979M 0% /dev tmpfs 200M 17M 184M 9% /run /dev/sda1 38G 16G 20G 45% / tmpfs 999M 152K 999M 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 999M 0 999M 0% /sys/fs/cgroup tmpfs 200M 120K 200M 1% /run/user/1000 [email protected]:~$ free -m total used free shared buff/cache available Mem: 1997 1585 87 22 325 246 Swap: 2045 972 1073 [email protected]:~$ speedtest-cli Retrieving speedtest.net configuration... Testing from BBNL (220.127.116.11)... Retrieving speedtest.net server list... Selecting best server based on ping... Hosted by E-Infrastructure & Entertainment India Pvt. Ltd (Bangalore) [27.56 km]: 9.772 ms Testing download speed................................................................................ Download: 9.96 Mbit/s Testing upload speed................................................................................................ Upload: 10.26 Mbit/s [email protected]:~$ exit exit Script done on Tuesday 04 July 2017 08:41:48 AM IST
Add A Custom Filename To Script Output
By default the script command save the output in the name of
typescript. You can add a custom file name by passing a file name followed by script command, retaining the prior contents. In my case, i have input a file name called
2g-terminal.txt, you can change the filename as per your wish.
$ script 2g-terminal.txt Script started, file is 2g-terminal.txt
Append The Output To File
When you run the script command again without
-a option that will overwrite the output file. To append the output to file, use
-a option followed by the filename.
$ script -a 2g-terminal.txt Script started, file is 2g-terminal.txt
Store A Single Command Output
To store a single command output instead of an interactive shell session, use the
-c option with script command. Say for example, Lynis – Security Auditing tool has 1000+ lines output, so this option very helpful for such kind of requirement.
$ script -c 'whoami' magi-terminal.txt Script started, file is magi-terminal.txt magi Script done, file is magi-terminal.txt
Run The Script Command On Quit Mode
-q option with script command to avoid script started and script done message on terminal while run the script command.
$ script -c 'whoami' -q magi-terminal.txt magi
Script Man Page
To know other options and purpose, nagivate to script command man page.
$ man script or $ script --help