How to Store Linux terminal output in the file automatically

In the article, we are going to discuss how to store Linux Terminal output into Log file or text file automatically or how to record Linux terminal. Linux utilities like script, scriptreplay, etc. can be used to record commands and their output printed on your terminal during specific terminal session.

The linux command-line utility history command can be used to view previously used command on the terminal, but it can not store the output of that all commands.

Whenever we are working on special projects and lazy about to redirect the output of the commands to text files every time using (>),(>>) or command utilities like tee, etc. Here is the solution for you guys by which the full terminal session will be recorded by only one command until you stop it. It will record all the commands and their outputs to a user-defined log file.

script Command Utility

Usage:
  script [options] [file]
Make a typescript of a terminal session.
Options:
  -a, --append                  append the output
  -c, --command <command>       run command rather than interactive shell
  -e, --return                  return exit code of the child process
  -f, --flush                   run flush after each write
      --force                   use output file even when it is a link
  -o, --output-limit <size>     terminate if output files exceed size
  -q, --quiet                   be quiet
  -t[<file>], --timing[=<file>] output timing data to stderr or to FILE
  -h, --help                    display this help
  -V, --version                 display version

How to Store Linux Terminal output using script Command

script command will store all commands and their outputs of Linux terminal in a text file or log file.

Run below script command with the output file name to start recording all the data of the terminal and store it in file.

$ script report.txt

Command will Give output as “Script started, file is report.txt”, that means the recording of the data is started successfully.

Now run any command that you want, this all the data will be stored in report.txt file automatically.

When you are done with the work, you can stop the utility by exit command.

$ exit

This will give output alert “Script done, file is report.txt”, that means all the data captured and stored in report.txt log file.

How to Store Linux terminal output in the file automatically

Now, you can use any text editor or cat command to view the text file in which data is stored.

$ cat report.txt

This will show the output as below,

root@kali:~# id
 uid=0(root) gid=0(root) groups=0(root)
root@kali:~# pwd
 /root
root@kali:~# nmap 127.0.0.1 -v
 Starting Nmap 7.80 ( https://nmap.org ) at 2020-06-09 12:11 IST
 Initiating SYN Stealth Scan at 12:11
 Scanning localhost (127.0.0.1) [1000 ports]
 Stats: 0:00:00 elapsed; 0 hosts completed (1 up), 1 undergoing SYN Stealth Scan
 SYN Stealth Scan Timing: About 1.00% done; ETC: 12:11 (0:00:00 remaining)
 Discovered open port 21/tcp on 127.0.0.1
 Completed SYN Stealth Scan at 12:11, 0.07s elapsed (1000 total ports)
 Nmap scan report for localhost (127.0.0.1)
 Host is up (0.000014s latency).
 Not shown: 999 closed ports
 PORT   STATE SERVICE
 21/tcp open  ftp
 Read data files from: /usr/bin/../share/nmap
 Nmap done: 1 IP address (1 host up) scanned in 0.18 seconds
            Raw packets sent: 1000 (44.000KB) | Rcvd: 2001 (84.044KB)
root@kali:~# exit

scriptreplay Command Utility

This was a simple static text file captured data. Now, if we want to replay the terminal session like video, then we can use another utility called scriptreplay with script.

First of all we have to record the session commands using script with time options. Run the below command to start the capturing data.

$ script --timing=time.txt report.txt

Use exit command when you are done. The command will create two files. One is report.txt for data storage and another is time.txt for time-line storage.

Then run scriptreplay command with the data file and time-line file, to view the captured terminal session. This will play captured session as same as the video.

$ scriptreplay --timing=time.txt report.txt

This will play the recorded session. This will show like we are viewing a video in the terminal.

How to Store Linux terminal output in the file automatically

So, that’s all guys. If you have any doubts then you can ask using the comment section below of the page.