Overview
script records all activities performed in the terminal into a text file. This can be used for training, troubleshooting, or auditing purposes, and recorded sessions can be replayed using the scriptreplay command.
Key Features
- Records all input and output of a terminal session
- Saves to a 'typescript' file by default
- Can append to existing files
- Can record only the output of specific commands
- Supports recording and replaying session timing information
Key Options
The main options for the script command affect how the recording is done, how output files are managed, and the recording of session information.
Recording and File Management
Generated command:
Try combining the commands.
Description:
`script` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Learn how to effectively record terminal sessions through various usage examples of the script command.
Basic Usage (Start and End Recording)
script
# All commands and output from now on will be recorded.
ls -l
pwd
exit
When you run script, it starts recording all input and output of the current session into a file named 'typescript'. To stop recording, type 'exit'.
Record Session to a Specific File
script my_session.log
# Session recording...
exit
Record the session to a file with a name of your choice, instead of the default 'typescript' file.
Append Session to an Existing File
script -a my_session.log
# Additional session recording...
exit
Append new session content to an already existing log file without overwriting the previous content.
Record Only a Specific Command
script -c "ls -la /tmp" ls_tmp.log
Instead of starting an interactive shell, this records only the execution results of a specified single command and exits immediately.
Record with Timing Information
script -t 20231027_timing.log my_session_with_timing.log
# Session recording...
exit
Records the session content along with timing information for each output into separate files. These two files are used with scriptreplay to play back the session.
Tips & Precautions
Tips and precautions for using the script command more effectively and preparing for potential issues.
Viewing Recorded Sessions
The recorded 'typescript' file is a plain text file, so you can view its content using commands like `cat`, `less`, or `more`. However, since it may contain special characters or control codes, `less -R` or `cat -v` might be more useful.
- View default file: `cat typescript`
- View specific file: `less my_session.log`
Replaying Sessions (scriptreplay)
If you recorded timing information with the `-t` option, you can use the `scriptreplay` command to play back the recorded session in a real terminal. This is very useful for demos or training.
- Replay command: `scriptreplay 20231027_timing.log my_session_with_timing.log`
Security and Privacy
Since script records everything entered, passwords or sensitive information can be saved directly into the file. Ensure appropriate access permissions are set for recorded files and exercise caution when performing sensitive operations.
- Caution: Be aware that sensitive information like passwords, API keys, etc., may be recorded.
- Recommendation: Protect recorded files by setting permissions, e.g., `chmod 600`.
Caution on Abrupt Termination
If the terminal session is abnormally terminated during script recording (e.g., by pressing Ctrl+C multiple times to kill the script process itself) or if the power is cut, the recording file may be corrupted or incompletely saved. It is always recommended to terminate normally using the `exit` command.