The script
command in Linux is used to record a terminal session. It captures everything shown on the terminal screen, including the commands executed and their outputs. This can be useful for troubleshooting, documentation, or creating logs of terminal interactions.
Basic Syntax:
script [options] [file_name]
file_name
: The file where the session will be saved. If not specified, it will save to a default file namedtypescript
.options
: Optional flags to control the behavior of thescript
command.
Example 1: Start a Session and Save to Default File
script
This command starts recording the terminal session. All commands executed and their output will be saved in the file typescript
in the current directory. To stop recording, type exit
.
Example 2: Save Session to a Specific File
script mysession.log
This command starts recording the terminal session and saves it to mysession.log
. To stop the session, type exit
.
Example 3: Recording with Timestamps
You can use the -t
option to include timestamps in the output.
script -t 2> timing.log mysession.log
-t
: Generates a timing file.2> timing.log
: Saves timing information to the filetiming.log
.
This will record both the session output and timing details of each command.
Example 4: Quiet Mode (Suppresses Start/End Messages)
script -q mysession.log
The -q
option suppresses the introductory message when starting and stopping the recording. It results in a cleaner log file.
Example 5: Script with Shell Commands
You can also pass a command to script
to execute it within the session.
script -c "ls -l" mysession.log
This runs the ls -l
command inside a session and saves the output to mysession.log
. Once the command completes, the session ends.
Example 6: Include Error Messages
By default, error messages are not captured. To include them, you can use the -a
option to append the output to an existing file.
script -a mysession.log
This will append the session’s output to the file mysession.log
instead of overwriting it.
Example 7: Recording an Entire Session in Real-Time
If you want to log an interactive session (e.g., a script execution or series of commands), simply run script
and perform actions normally. For example:
script session_output.txt
Then type commands like:
echo "Hello, World!"
ls /usr
After finishing, type exit
, and the terminal session will be recorded in session_output.txt
.
Conclusion:
The script
command is versatile for recording terminal sessions. Use it to capture command history, document workflows, or troubleshoot terminal activities.