How to Record Your Terminal Session Using the script Command in Linux

When you are executing a highly complex database migration or compiling a massive kernel module on a Linux server, simply relying on terminal scrollback is mathematically insufficient. If the process crashes and the terminal buffer overflows, the exact sequence of commands and errors is permanently destroyed. To force the Linux kernel to intercept your entire shell environment and mathematically record every single keystroke and output vector into a pristine typescript file, you must deploy the script command.

Understanding the Session Recording Architecture

The script command does not merely redirect stdout (standard output). It algorithmically spawns a secondary pseudo-terminal (pty) sub-shell. Everything you type, every escape sequence, every backspace, and every single pixel of output rendered by the system is intercepted by this sub-shell and physically written to disk in real-time, creating an absolute forensic record of your session.

Initializing the Forensic Capture

To begin recording your terminal session, open your terminal and simply type the command followed by the target filename (if you omit the filename, the engine defaults to a file named typescript).

script migration_log_v2.txt

The exact millisecond you press Enter, the engine outputs Script started, file is migration_log_v2.txt. You are now operating inside the forensic sub-shell. You can execute complex pipelines, edit files with nano, or compile code. The engine silently captures the entire geometric rendering.

Terminating the Sub-shell Vector

When your critical operation is complete, you must explicitly terminate the sub-shell to flush the final buffer to the physical disk. Type exit or press Ctrl+D.

The engine will output Script done, file is migration_log_v2.txt. You can now use standard commands (like cat, less, or grep) to mathematically analyze the massive file. Because the engine captures raw escape sequences, if you cat the file in a standard terminal, it will accurately replay the color formatting and cursor movements exactly as they occurred during the live session.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.