The script command in Linux is a powerful utility used to record terminal sessions. It allows users to capture everything that happens in a terminal session, making it useful for documentation, troubleshooting, and sharing terminal interactions. This article will delve into the script command, its syntax, usage, and some practical examples
How does script command work
The script command creates a typescript file that records all terminal input and output. This file can later be reviewed, making it ideal for various purposes, such as creating tutorials, debugging, or simply keeping a log of your command-line activities
Use Cases
- Documentation: The script command is invaluable for creating documentation. You can record the steps to perform a particular task and later transcribe or share the typescript file.
- Troubleshooting: When encountering issues, recording your terminal session can help you capture error messages and output, providing context for debugging.
- Teaching: For educators, the script command offers a way to create tutorials or instructional materials, showing exactly what commands were used and their results.
Basic Syntax
The basic syntax of the script command is as follows:
script [options] [file]
- file: This is the name of the file where the session will be recorded. If no file is specified, the default file name is typescript.
- options: Various flags that modify the behavior of the command
How to Use the script Command
Basic Text based recording
Starting a Session
To start recording your terminal session, simply type:
script
This will start recording everything you type and the output displayed in your terminal. The default file typescript will be created in your current directory.
Specifying a File Name
To specify a different filename for the output, use:
script mysession.log
This command will create a file named mysession.log.
Ending a Session
To end the recording, simply type:
exit
Alternatively, you can press Ctrl+D. After exiting, you will find your session recorded in the specified file.
Recording the output specific command
You can also log the output of a specific command without recording an entire session. For instance:
script -c "ls -l" list.log
Removing the initial starting and ending statements
- -q for removing the initial starting and ending statements when using script.
script -q <script file>
Appending the output of previous recording
-a for appending new commands and output to a previously-used file.
script -a <script file>
Example Usage
Here’s a practical example of using the script command:
1 . Start recording:
script session.log
2. Execute a few commands:
echo "Hello, World!"
ls -l
pwd
3. Exit the session:
exit
4. Review the recorded session:
You can view the contents of session.log using:
cat session.log
Now your output will look just like this
Script started on 2024-11-03 09:26:10+05:30 [TERM="xterm-kitty" TTY="/dev/pts/0" COLUMNS="172" LINES="42"]
echo "hello world"
hello world
ls -l
pwd
Script done on 2024-11-03 09:26:28+05:30 [COMMAND_EXIT_CODE="0"]
Time based recording
This is the main selling point of the script command as it gives you ability to record terminal output into two separate files: one for capturing the actual output and another for storing timing information. After recording, you can replay this session, allowing you to experience it as if you were watching a video. This feature is particularly useful for reviewing interactions or demonstrating processes step-by-step.
To do this we will use -t flag of script
# To reocrd
script --t=tfile -q sfile
# To replay
scriptreplay --timing=tfile sfile
To replay the recorded session, we use the scriptreplay command, providing both the timing file and the output file generated by the script command. This allows us to experience the session as it originally unfolded, complete with accurate timing.
Conclusion
The script command is a simple yet powerful tool for recording terminal sessions in Linux. Whether you’re documenting a process, troubleshooting an issue, or teaching others, script allows you to capture every detail of your command-line interactions. By mastering this command, you can enhance your workflow and create valuable resources for yourself and others.