Using the `script` Command to Record Terminal Sessions (with examples)
- Linux
- November 5, 2023
Are you tired of manually copying the output of your terminal sessions? Do you want an easier way to save all the commands, outputs, and error messages? Look no further! The script
command is here to help.
1: Record a new session to a file named typescript
in the current directory
script
Motivation:
By using the script
command without any arguments, you can easily start recording your terminal session to a file named typescript
in the current directory. This allows you to keep track of everything that happens in your session, including commands, command outputs, and error messages.
Explanation:
The command script
starts a new session and begins recording all terminal output. By default, it saves the output to a file named typescript
in the current directory.
Example Output:
No output is displayed on the terminal when the script
command is executed.
2: Record a new session to a custom filepath
script path/to/session.out
Motivation: In some cases, you may want to save your session output to a specific file path. This could be useful if you want to organize your session recordings, save them to a different directory, or share them with others.
Explanation:
By providing a custom filepath as an argument to the script
command, you can specify where you want the session output to be saved. In the example above, we save the output to the file session.out
located in the path/to/
directory.
Example Output:
No output is displayed on the terminal when the script
command is executed.
3: Record a new session, appending to an existing file
script -a path/to/session.out
Motivation: Sometimes, you may need to add more content to an existing session recording. By appending to an existing file, you can keep all your session output in one place, even if you start multiple sessions or gather output from different sessions over time.
Explanation:
By using the -a
option along with a custom filepath, the script
command appends the session output to an existing file instead of overwriting it. In the example above, the output is appended to the file session.out
located in the path/to/
directory.
Example Output:
No output is displayed on the terminal when the script
command is executed.
4: Record timing information (data is outputted to stderr
)
script -t 2> path/to/timingfile
Motivation: In addition to recording the terminal output, you may want to keep track of the timing information for your commands. This can be useful for performance analysis, troubleshooting, or simply understanding how long certain operations took.
Explanation:
By using the -t
option, the script
command records timing information for each command executed in the session. The timing data is redirected to the standard error stream (stderr
). In the example above, the timing information is saved to the file timingfile
located in the path/to/
directory.
Example Output:
No output is displayed on the terminal when the script
command is executed. However, the timing information for each command is saved in the file specified (timingfile
).