How to Save the Terminal Output to a File?

Linux is a command line-based executive that carries out most of the tasks through commands. Sometimes, the users want to get the output of the specific command into an external file to process the information lately. In Linux, various methods are carried out to save the terminal output to a file.

This article will demonstrate the methods to save the output of the terminal to the file with the following outline:

Method 1: Save Terminal Output to a File Through Redirection

The redirection save the output of the command in the file by redirecting it. The syntax for using the redirection operator is given below.


$ [Command] > [File Name]
$ [Command] >> [File Name]

The first command “>” will add and replace the content of the file with the output. While the “>>” will append the output to the existing content. 

Example 1: Redirecting the Output

The following command will save the output of the “ls -l” command to a text file using the “>” operator:

$ ls -l > Terminal_Output.txt

After executing the above command, the output of the “ls -l” command will be saved in the “Terminal_Output.txt” file as can be verified below:

$ cat Terminal_Output.txt

The output of the “ls -l” command has been saved.

Example 2: Append the Output to an Existing Content of the File

In the home directory, the output.txt already exists as can be seen in the below image:


If you want to append data in an already existing file, you use the “>>” operator in the command as follows:

$ ls -l >> output.txt

Lets’ verify the results by displaying the content of the “output.txt” file:

$ cat output.txt

Note: The difference between “>” and “>>” is that the “> ” operator will overwrite or create the file if it does not exist. While the “>> operator” appends data in the already existing file.

Example 3: Save the “stdout” and “stderr” to a File

If the Linux command is not used correctly, it throws an error instantly (known as standard error). The “&>” operator allows you to get that standard error and the standard output in an external file. 

The syntax of these operators is as follows:  


$ [command] &> [File Name]  #override or creating the File
$ [Command] &>> [File Name] #appending data in existing File

Let’s execute the cd command with the wrong name of the directory and check that the & operator will pass the stderr output to the file:

$ cd desktop &> newfile.txt

The error has been saved in the file.

Verify the output of the “newfile.txt”:

$ cat newfile.txt

The file “newfile.txt” stores both the error message.

Likewise, the same operation can be performed for the existing file using the “>>” operator as given in the above syntax.

Method 2: Save Terminal Output to a File Using the tee Utility

The tee command will also give the facility to store the terminal command’s output in the file. Users can use the pipe(|) command to send the output of the command as an input to the tee command. The syntax for the tee command is given below:


$ [command] | tee [Option] [File Name]

Type the tee keyword, “options” along with the tee command, and the file name to store the output.

Example: Save Output to a New File 

Let’s check the execution of the tee command using the below example in which we are saving an echo statement to the file:

$ echo "itslinuxfoss" | tee file.txt

Let’s check the output of the “file.txt” using the “cat” utility:

$ cat file.txt

The file “file.txt” contains the echo message.

Example 2: Append the Output to an Existing File

The “a” option of the “tee” command appends the data to the existing content of the file. 

$ lsblk | tee -a file.txt

Check the output of the fle.txt:

$ cat file.txt

The cat file output has been appended with the new output.

Method 3: Save Terminal Output to a File Using the script

There is another method to save the terminal command’s output in the file which is using the “script” command. The script command will record the stdin and stdout until the user type “exit” and store the output to the file. The syntax for using the script is given below:


$ script [File Name]

Type the script along with the file name.


To start the script in the terminal is obtained as follows:

$ script itslinuxfoss.txt

The script will be started and the user can perform the tasks using the commands.

Once you perform the all operations you want to store, type the exit in the terminal and it will print the whole log into the file:

$ exit

The script is done now.

The file has recorded all the operations that happened in the particular session in the file as shown in the below image:


To save the output of the terminal into a file, three methods are utilized which are Redirection Operator, tee utility, or script command. The redirection operator gets the standard error and standard output to the external file. While the tee utility is used with a “pipe” operator with the command to get the output. The third method, i.e., the script command saves the complete session of the terminal to a file. This write-up has illustrated the various methods to save the output of the terminal to the file.