Record command output

terminalI have faced this situation innumerable times – started a compilation, it failed and the failure started so high up it seems an infinity to reach there by scrolling up. And if the terminal scrollback is limited, you never find the spot where the error started.

In this article we will discuss a few ways to record terminal output of a command so that you can view or edit the output later.

1. Here’s a small bash script to log the output (including errors) from a single command in /tmp/lastlog:

"$@" 2>&1 | tee /tmp/lastlog

Copy the content in a file (say lastlog) and make it executable. Example usage:

$ lastlog make mytarget

This doesn’t need any extra utilities. The advantage is a clean output without any escape sequences. Open in vim and view or edit.

2. Install script on Ubuntu:

$ sudo apt-get install bsdutils

Script can be used to record and replay terminal sessions with timestamps. However, here we will use it to log screen activities only. To start a new recorded session, run:

$ script

The output will be stored in the file typescript, in the same directory where you ran script.

The output has ANSI escape sequences in it. It is difficult to comprehend if you open it in vim. However, you can use cat or more which render the output correctly. And sometimes the colourful output is easier for debugging (check out colormake).

In order to use vim, you can install the vim plugin Improved AnsiEsc. To view the output correctly, open the file in vim and type the following in command mode:


However, I was NOT happy with the results. Many escape sequences were left behind.

3. Install screen on Ubuntu:

$ sudo apt-get install screen

Start a session with logging:

$ screen -L

The session will be logged in screenlog.n file. The output is similar to that of script. Use the same techniques.

If you want a cleanse the log from screen or script, use Dewtall’s perl script. Save the following snippet as cleanse and mark it executable:

while (<>) {
    s/ \e[ #%()*+\-.\/]. |
       \r | # Remove extra carriage returns also
       (?:\e\[|\x9b) [ -?]* [@-~] | # CSI ... Cmd
       (?:\e\]|\x9d) .*? (?:\e\\|[\a\x9c]) | # OSC ... (ST|BEL)
       (?:\e[P^_]|[\x90\x9e\x9f]) .*? (?:\e\\|\x9c) | # (DCS|PM|APC) ... ST
       \e.|[\x80-\x9f] //xg;
       1 while s/[^\b][\b]//g;  # remove all non-backspace followed by backspace

To clean the output, run:

$ ./cleanse typescript > cleanlog

cleanlog will now have the clean output.

4. logsave

logsave comes with other utilities for extended filesystems. You can use it to achieve the same result as we did with the script in option 1. If not installed already, install logsave on Ubuntu, run:

$ sudo apt-get install e2fsprogs


$ logsave /tmp/lastlog make mytarget


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s