Mostly, we, all, use the Up/Down arrow keys or the history command to find out the previously entered commands in Terminal. Unfortunately, we can’t view the output of the previously entered commands using the above methods. We can just see the commands only. If you are working on a GUI desktop, you can have a plenty of applications/tools to see or record your Terminal activities, including the previously entered commands and their corresponding output. But, What if you have only CLI mode servers? Here is where script command comes in handy.
What is Script command?
From the man pages;
Script makes a typescript of everything printed on your terminal. It is useful for students who need a hardcopy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1). If the argument file is given, script saves all dialogue in file. If no file name is given, the typescript is saved in the file typescript.
For more details. refer the man pages.
man script
Usage
script command comes pre-installed(even in minimal installation) on most Linux distributions.
Let us see some examples.
First, run the following command with parameter ‘a’. It means the recording will be saved in a specified file. Look at the following command:
script -a sk_terminal
Sample output:
Script started, file is sk_terminal
The script is started and the output will be stored in the file namely sk_terminal.
Now, let us enter some commands.
ls -a
Sample output:
. .bash_history .bashrc shinken-2.2 .tcshrc .. .bash_logout .cshrc shinken-2.2.tar.gz anaconda-ks.cfg .bash_profile .pki sk_terminal
mkdir test
useradd unixmen
passwd unixmen
Sample output:
Changing password for user unixmen. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully.
rmdir test
uname -a
Sample output:
Linux server1.unixmen.local 3.10.0-123.9.3.el7.x86_64 #1 SMP Thu Nov 6 15:06:03 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
date
Sample output:
Mon Mar 9 13:24:37 IST 2015
OK, It’s enough. Now, exit the recording using command:
exit
Sample output:
exit Script done, file is sk_terminal
The script command is exited. We will now check what we were doing before in Terminal.
Viewing the script command output
cat sk_terminal
Sample output:
Script started on Monday 09 March 2015 01:19:17 PM IST [root@server1 ~]# ls -a . .bash_history .bashrc shinken-2.2 .tcshrc .. .bash_logout .cshrc shinken-2.2.tar.gz anaconda-ks.cfg .bash_profile .pki sk_terminal [root@server1 ~]# mkdir test [root@server1 ~]# useradd unixmen [root@server1 ~]# passwd unixmen Changing password for user unixmen. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. [root@server1 ~]# rmdir test [root@server1 ~]# uname -a Linux server1.unixmen.local 3.10.0-123.9.3.el7.x86_64 #1 SMP Thu Nov 6 15:06:03 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux [root@server1 ~]# date Mon Mar 9 13:24:37 IST 2015 [root@server1 ~]# uname -a Linux server1.unixexit exit Script done on Monday 09 March 2015 01:25:44 PM IST
As you see above, the previous activities on the Terminal are recorded including the commands with their corresponding answers. The commands are marked in bold letters.
That’s it. Cheers!
We thank you our Unixmen regular reader Mr.Asutosh to let us know about this command. Much appreciated.