How to Run “script” Command in Linux?

14

The script command is used to typescript or record all terminal processes. After running the script command, it begins recording everything that appears on the screen, including inputs and outputs, until it exits.

If you don’t specify any argument, the “typescript file” will be created in the directory to save the terminal record.

The script command records both standard input/output and time of execution.

This post describes what the “script” command is and how it works with various options.

Syntax:

Follow the below-given syntax of “script” command:

$ script [option] [filename]

Let’s discuss some examples.

Example 1:

Using Script Command Without Any Argument

To start, type “script” without specifying any parameters. If no parameter is specified, script will create a “typescript” file in the directory to save the record.

The “script” would begin recording that can be stopped anytime with the “exit” command. Various scripts can be run in the meantime.

Simply run the exit command to end typescript, and the script will end the capture process:

How to Run “script” Command in Linux? 1620435575 934 How to Run script Command in Linux

Example 2:

Using “script” Command with Argument

Specify any file name as an argument. For instance, I am creating a text file by the name of “linuxhint.txt”. Run the following command:

Then run some commands in the terminal and type, “exit” to end the capturing process.

How to Run “script” Command in Linux? 1620435575 184 How to Run script Command in Linux

Now, open the “linuxhint.txt” file.

Output:

The below content is the output of the file linuxhint.txt, which was created by the script command.

How to Run “script” Command in Linux? 1620435576 968 How to Run script Command in Linux

1) -c option:

This option is used to get the information of a particular command instead of all commands running in an interactive shell with the file name specified as an argument. After successful execution, the script will exit automatically.

For example, to get a calendar, use:

$ script –c cal linuxhint.txt

How to Run “script” Command in Linux? 1620435576 549 How to Run script Command in Linux

Output:

This command will show you the calendar in a txt file.

How to Run “script” Command in Linux? 1620435577 62 How to Run script Command in Linux

2) -a Option

This option appends the output to the file while keeping the previous content. The content of both files is separated by a space line.

Take the following command into consideration:

$ script -a linuxhint.txt

How to Run “script” Command in Linux? 1620435577 161 How to Run script Command in Linux

Output:

Display the output with the previous running script.

How to Run “script” Command in Linux? 1620435578 838 How to Run script Command in Linux

3) -t, –timing[=] Option

This option is used to record terminal operations line by line, which looks like a video. The “scriptreplay” command is used to play back the recorded file.

We need to give a file a name to record the activity. In this example the filename is “linxhint”:

$ script -timing=time_log linuxhint

How to Run “script” Command in Linux? 1620435578 455 How to Run script Command in Linux

Output:

To replay the “script” command, use:

$ scriptreplay –timing=time_log linuxhint

How to Run “script” Command in Linux? 1620435579 883 How to Run script Command in Linux

4) –force Option

To save the script in some specific directory, use the “–force” option. Run the below force command:

$ script –force /home/aqsa/linux.txt

How to Run “script” Command in Linux? 1620435579 930 How to Run script Command in Linux

Output:

How to Run “script” Command in Linux? 1620435580 369 How to Run script Command in Linux

5) -e Option:

This option returns the child process, type the below command:

$ script -e linuxhint.txt

How to Run “script” Command in Linux? 1620435580 497 How to Run script Command in Linux

In the above image, linuxhint2.txt is the child of linuxhint.txt, and linux.txt is a parent process.

Output:

How to Run “script” Command in Linux? 1620435581 783 How to Run script Command in Linux

6) –flush Option

The “-f” or “–flush” options are used to flush the output. It can be used for teleoperation.
Here, the command is:

$ script –flush linuxhint.txt

How to Run “script” Command in Linux? 1620435581 353 How to Run script Command in Linux

7) -q quiet Option

The “-q” option hides the message when the script has started and exits:

How to Run “script” Command in Linux? 1620435582 968 How to Run script Command in Linux

8) –help Option

This option is used to get help information. It will display all the “script” command related options with one line description:

How to Run “script” Command in Linux? 1620435582 90 How to Run script Command in Linux

9) –V/–version Option

This option is used to check the version of “script” command:

How to Run “script” Command in Linux? 1620435583 117 How to Run script Command in Linux

Conclusion:

Script is a command-line utility used to get the input and output of all running commands in the text file. It captures all the executed activities in the terminal. Moreover, it prints both input and output in the text file. “Script” command is used to make a copy of executed commands, which should be kept in your notebook and submitted as part of an assignment. Through this post, we have seen the functionality of different “script” options with examples.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More