script Command in Linux
Overview
The script command in Linux is an incredibly useful tool that provides a way to record a shell session for later reference. It makes a typescript of everything printed on your terminal, proving handy for educational purposes or tracking your actions in a terminal session.
Syntax of script Command in Linux
The basic syntax of the script command in Linux is as follows:
Where:
- options: These are optional flags that can modify the behavior of the script command.
- file: This is the optional name of the file where the session will be recorded. If omitted, the output is saved to a file named 'typescript' in the current directory.
Options in script Command in Linux
-
-a, --append: Append the output to 'file' or 'typescript', retaining the prior contents.
For example -
This command appends the output of the script command to 'session.log'.
-
-q, --quiet: Quiet mode, don't display the start and stop notifications.
For example -
This command starts a new session with the script command, and does not display the usual start and stop notifications.
-
-c, --command: Run the 'command' rather than an interactive shell.
For example -
This command executes 'ls -la', and saves the output to 'file.log'.
Example Usages
-
Basic usage of script command:
Output:
Explanation: This command starts a new recording session. The output of this session is stored in the 'typescript' file.
-
Usage with filename:
Output:
Explanation: This command starts a new recording session, with the output stored in the 'mysession.log' file.
Tips
-
Be careful when recording scripts that contain sensitive information, such as passwords, as the script command will capture all input and output.
-
Consider using 'exit' command to properly end the script recording session.
Advanced Use Cases of script Command in Linux
-
Using script command with date command:
Output:
Explanation: This command records the terminal session and saves the output into a file with the current date as part of the name.
-
Use script command with -c option to record specific command:
Explanation: This command runs 'ls -la' and records its output into 'directory_listing.log' file.
-
Use script command with tee to view and save the output at the same time:
Explanation: This command runs 'ls -la', displays its output and simultaneously saves it into 'file.log'.
Conclusion
-
The script command in Linux is a powerful tool to record terminal sessions.
-
It provides various options like -a, -q, and -c to modify the command's behavior.
-
Script command is beneficial for educational purposes, documentation and tracking your actions.