Save Linux terminal session (script)

During daily work, sometimes we perform tasks that we would like to document properly.entrada Linux script

In this post we will see how to save an entire Linux terminal session, so that we can subsequently include any documentation.

The command we use is:

script

Typically the most popular Linux distributions it is not installed by default.

In this link you can see the page man:

http://www.unix.com/man-page/centos/1/script/

The syntax is:

 script [-a] [-c COMMAND] [-f] [-q] [-t] [file]

Let’s see only 2 parameters:

  • file, It indicates where the catch of the executed commands are saved.
  • -a, It indicates that an existing file is added.

If it is run without any parameters is generated a file name typescript, which includes all executed.

This command stored in the file:

  • The standard input, stdin
  • The standard output, stdout
  • The standard error, stderr
  • Any other special characters, keystrokes, cursor, carriage returns, tabs, etc … We’ll see later how we get a clean output.

An example:

Source   
[oracle@oradb soft]$ ls
temp.txt
[oracle@oradb soft]$ script captura.txt
Script started, file is captura.txt
[oracle@oradb soft]$ ls -la
total 12
drwxr-xr-x  2 oracle oinstall 4096 Nov 20 22:17 .
drwx------ 28 oracle oinstall 4096 Nov 20 22:16 ..
-rw-r--r--  1 oracle oinstall    0 Nov 20 22:17 captura.txt
-rw-r--r--  1 oracle oinstall   33 Nov 20 22:16 temp.txt
[oracle@oradb soft]$ cat temp.txt
This is a test file.
End of file
[oracle@oradb soft]$ grep End temp.txt
End of file
[oracle@oradb soft]$ exit
exit
Script done, file is captura.txt
[oracle@oradb soft]$ ls
captura.txt  temp.txt

We now have a file called “captura.txt with everything executed, if for example we edit with vi will see many special characters messing the output, this is because I deleted several times and rewritten.

The content of “captura.txtseen vi is:

Source   
Script started on Fri 20 Nov 2015 10:17:07 PM CET
^[]0;oracle@oradb:~/soft^G^[[?1034h[oracle@oradb soft]$ ls -la^M
total 12^M
drwxr-xr-x  2 oracle oinstall 4096 Nov 20 22:17 ^[[0m^[[01;34m.^[[0m^M
drwx------ 28 oracle oinstall 4096 Nov 20 22:16 ^[[01;34m..^[[0m^M
-rw-r--r--  1 oracle oinstall    0 Nov 20 22:17 captura.txt^M
-rw-r--r--  1 oracle oinstall   33 Nov 20 22:16 temp.txt^M
^[[m^[]0;oracle@oradb:~/soft^G[oracle@oradb soft]$ cat c^H^[[Ktemp.txt ^M
This is a test file.^M
End of file^M
^[]0;oracle@oradb:~/soft^G[oracle@oradb soft]$ grep End | ^H^[[K^H^[[Kcaptura.txt^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[Kcaptura.txt^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[K^H^[[Ktemp.txt^M
End of file^M
^[]0;oracle@oradb:~/soft^G[oracle@oradb soft]$ exit^M
exit^M
 
Script done on Fri 20 Nov 2015 10:17:44 PM CET

A disaster, to get a clean output can do simply with cat:

Source   
[oracle@oradb soft]$ cat captura.txt
Script started on Fri 20 Nov 2015 10:17:07 PM CET
[oracle@oradb soft]$ ls -la
total 12
drwxr-xr-x  2 oracle oinstall 4096 Nov 20 22:17 .
drwx------ 28 oracle oinstall 4096 Nov 20 22:16 ..
-rw-r--r--  1 oracle oinstall    0 Nov 20 22:17 captura.txt
-rw-r--r--  1 oracle oinstall   33 Nov 20 22:16 temp.txt
[oracle@oradb soft]$ cat temp.txt
This is a test file.
End of file
[oracle@oradb soft]$ grep End temp.txt
End of file
[oracle@oradb soft]$ exit
exit
 
Script done on Fri 20 Nov 2015 10:17:44 PM CET

Now the output is perfect, now we just need the desire to document 🙂

Leave a Reply