Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

script Command in Linux



Capturing terminal sessions can be invaluable for record-keeping, troubleshooting, and sharing command outputs. The script command in Linux is a highly effective tool for this purpose, providing a straightforward way to record terminal activities.

The script command is a versatile utility designed to record terminal sessions. Whether for documenting commands and their outputs, creating tutorials, or troubleshooting, script captures all terminal activity into a file, making it an essential tool for users and administrators alike. Mastering the script command enhances your ability to document and share terminal interactions efficiently.

Table of Contents

Here is a comprehensive guide to the options available with the script command −

Syntax of script Command

The general syntax for the script command is −

script [options] [filename]

In this command, [filename] specifies the file where the terminal session will be recorded. If no filename is provided, the session is saved to a default file named typescript.

script Command Options

The script command includes several options to tailor its behavior −

Option Description
-a Adds the output to an existing file or to the default typescript, preserving the previous contents.
-c command Executes the specified command instead of an interactive shell. This is useful for capturing the output of programs that behave differently when their stdout is not a terminal.
-E, --echo when Controls the ECHO flag for the pseudoterminal. Options are always, never, or auto. The default is auto.
-e, --return Returns the exit status of the child process. It uses the same format as bash for termination signals (exit status is 128 + the signal number). The exit status is also saved in the typescript file.
-f, --flush Flushes output to the file after each write, useful for real-time monitoring. Note that this impacts performance, but you can use SIGUSR1 to flush logs on demand.
--force Allows the default output file typescript to be a hard or symbolic link. The command will follow symbolic links.
-B, --log-io file Logs both input and output to the same file. This option is effective only if --log-timing is also specified, otherwise, it's impossible to distinguish input from output streams.
-I, --log-in file Logs input to the specified file. If only --log-in is specified, output logging is disabled. Be careful, as it logs all input, including sensitive data like passwords.
-O, --log-out file Logs output to the specified file. The default file is typescript if neither --log-out nor --log-in is provided. Output logging is disabled if only --log-in is specified.
-T, --log-timing file Logs timing information to the specified file. There are two formats: classic (for single-stream logging) and multi-stream (for --log-io or combined --log-in and --log-out).
-m, --logging-format format

Forces the use of either advanced or classic timing log formats. The default is classic for output logging only and advanced when both input and output logging are requested.

  • Classic format − Contains two fields separated by a space—time elapsed since the previous output and the number of characters output.
  • Advanced (multi-stream) format − Includes an entry type identifier (Input, Output, Header, Signal), time elapsed since the previous entry, and type-specific data.
-o, --output-limit size Limits the size of the typescript and timing files to the specified size, stopping the child process once this limit is reached. The actual file size might exceed the specified value due to buffering.
-q, --quiet Suppresses the start and done messages.
-t[file], --timing[=file] Outputs timing data to standard error or to the specified file. This option is deprecated in favor of --log-timing.

Examples of script Command in Linux

Here are some practical scenarios where script can be effectively used −

  • Recording a Terminal Session
  • Specifying an Output File
  • Appending to an Existing File
  • Recording a Specific Command
  • Using Quiet Mode

Recording a Terminal Session

To record a terminal session, simply use the following command −

script

This command starts recording the session, and all terminal activity is saved to a file named typescript.

script Command in Linux1

Specifying an Output File

If you want to save the recorded session to a specific file, provide the filename as follows −

script mysession.log

This command records the terminal session into mysession.log.

script Command in Linux2

Appending to an Existing File

To append a new terminal session to an existing file without overwriting it, use the -a option −

script -a mysession.log

This ensures that new session recordings are appended to mysession.log.

script Command in Linux3

Recording a Specific Command

To record the output of a specific command, use the -c option −

script -c "ls -l" mysession.log

This command captures the output of ls -l and saves it to mysession.log.

script Command in Linux4

Using Quiet Mode

To suppress the start and stop messages when running the script command, enable quiet mode with -q

script -q mysession.log

This command records the session quietly, saving all activity to mysession.log without the standard notifications.

script Command in Linux5

Conclusion

The script command in Linux is a powerful utility for recording terminal sessions, providing an easy way to capture and document command outputs and terminal interactions. By understanding its purpose, syntax, options, and practical use cases, you can effectively utilize the script command to enhance your workflow, troubleshoot issues, and create detailed records of terminal activities.

Whether you need to document sessions, create tutorials, or share command outputs, mastering the script command ensures you have a reliable method for recording and replaying terminal interactions. Embrace the versatility of the script command to optimize your terminal documentation and improve your Linux administration skills.

Advertisements