Ubuntu

How to Save Terminal Output to a File in Ubuntu?

How to Save Terminal Output to a File in Ubuntu copy

When working in the Ubuntu Operating System, the users mostly run several commands. Running or Executing Commands in a Terminal can be hard to track and once the Terminal is closed, all the history of the executed commands in the Terminal is wiped out of the System. Therefore, it is crucial sometimes for the users to keep track of the commands they execute. Ubuntu offers various commands that can be used in the Terminal to save the output in a Text File for later use.

This article demonstrates every command that can be used to Save the Terminal Output to a File in Ubuntu.

How to Save Terminal Output to a File in Ubuntu?

The Terminal output can be redirected or saved to a Text File in Ubuntu through various methods. The output can then be used for later analysis or review, and also if you want to share the Terminal Output with any other user. The methods used to save the Terminal Output in a Text File are:

  • Saving the Output through Redirect.
  • Saving the Output using the “tee” Command.
  • Saving the Output using the “script” Command.

Each method or approach is discussed below in detail.

Method 1: Save the Terminal Output to a File through Output Redirection

With the “redirect” command, the output or an input of a file is used as an input for another file. In other words, you can save the Output generated by the Terminal in another text file. This output saved in another file can then be used as input for another file. The redirect command uses the Greater-than “>” symbol. The basic syntax of the “redirect” command in the Terminal is:

command > textFile.txt
command >> textFile.txt

The “>” and “>>” each have their own use, where the redirect “>” saves the Output by Overwriting the Text File, and the redirect “>>” saves the Output by Appending the Output in the Text File. Additional Redirects used are the “2>”, “2>>”, “&>”, and “&>>”.

Understanding the Redirects

Before using the redirects, it is crucial to understand the use of each redirect and the options used with the redirects. The Detailed Explanation of each redirect is discussed below.

command > textFile.txt

The Output of the Command redirected or saved to the File can be seen in the File but it will not be displayed in the Terminal. The Error will be visible or displayed in the Terminal but it will not be saved in the File. The “>” redirect Overwrites an Existing File, if any.

command >> textFile.txt

The Output of the Command redirected or saved to the File can be seen in the File but it will not be displayed in the Terminal. The Error will be visible or displayed in the Terminal but it will not be saved in the File. The “>>” redirect Appends an Existing File, if any.

command 2> textFile.txt

The Output of the Command redirected to the File can be seen in the Terminal but it will not be saved in the File. The Error will only be visible in the File. The “2>” redirect Overwrites an Existing File, if any.

command 2>> textFile.txt

The Output of the Command redirected to the File can be seen in the Terminal but it will not be saved in the File. The Error will only be saved in the File. The “2>>” redirect Appends an Existing File, if any.

command &> textFile.txt

The Output and the Error both won’t be visible in the Terminal. The File will contain the Output as well as the Error. The “&>” redirect will Overwrite an Existing File, if any.

command &>> textFile.txt

The Output and the Error both won’t be visible in the Terminal. The File will contain the Output as well as the Error. The “&>” redirect will Append an Existing File, if any.

Concluding the Redirects

Below, each Redirect is explained with examples for better understanding.

Approach 1: Saving the Terminal Output to an Already Existing File by Overwriting the File

The Redirect can be used to save the Terminal Output to a Text File by overwriting the existing text or content inside the Text File. In our case, we have the Text File “abc.txt” in our Home Directory:

To view the content or text inside the file, use the “cat” command:

cat abc.txt

The Text inside the “abc.txt” file will be displayed in the Terminal:

Now, to list the files in our current directory, and save the list in the file without displaying it in the terminal, the “>” redirect will be used. This will overwrite the existing Text or content inside the “abc.txt” file and save the Output of the Terminal, in our case, the list of files and directories in the current path, to the Text file “abc.txt”:

ls -l > abc.txt

The command line moves to the next line without displaying the output in the Terminal indicating the output was saved in the “abc.txt” file:

To verify if the output of the Terminal was saved to the “abc.txt” file, use the “cat” command again:

cat abc.txt

As you can see, the previous text or content inside the file “abc.txt” is overwritten, and the output of the “ls -l” command is saved in the Text File:

Approach 2: Saving the Terminal Output to an Already Existing File by Appending the File

The Redirect can also be used to save the Terminal Output to a Text File by Appending the Output to the existing text or content inside the Text File. We will use the “abc.txt” Text File again in our case. To view the Text inside the “abc.txt” Text File, we will use the “cat” command again:

cat abc.txt

The “abc.txt” Text File contains the Output of “ls -l” currently:

Now, to Append the text “Hello from Linux Ways”, we can use the echo command along with the “>>” Redirect. The “>>” redirect will ensure to Append the “echo” Output to the Text File:

echo "Hello from Linux Ways" >> abc.txt

The cursor moves to the next line ensuring the Output of the Terminal is saved in the file “abc.txt”:

To verify the Output saved in the Text File “abc.txt”, use the “cat” command again:

cat abc.txt

As you can see in the Terminal, the text is Appended in the file “abc.txt” without overwriting the previously added text:

Approach 3: Saving the Command Output Error to an Already Existing File by Overwriting the File

The Redirect can also be used to save the Error of any command to a Text File by Overwriting the existing text or content inside the Text File. We will use the “abc.txt” Text File again in our case, and to view the Text inside the “abc.txt” Text File, we will use the “cat” command again:

cat abc.txt

The Text Inside the “abc.txt” command can be seen in the Terminal:

Now, we will run the “errno” command in the Terminal. The “errno” command is not actually a command or the package is not installed in the Ubuntu System:

errno

As the errno is not a command, the Terminal is supposed to return an error saying the command is not found. Instead of displaying the Error in the Terminal, the “2>” redirect will save the error in the file “abc.txt” by overwriting the file:

errno 2> abc.txt

The cursor moves to the next line instead of displaying the output in the terminal. This means an error occurred and the error is saved in the text file “abc.txt” instead of showing in the Terminal:

To verify if the error was saved in the Text File “abc.txt”, use the cat command again

cat abc.txt

You can see the error is saved in the “abc.txt” file and the text inside the file was overwritten:

Approach 4: Saving the Command Output Error to an Already Existing File by Appending the File

The Redirect can also save the Error of any command by Appending the Error to the Text File. In our case, using the “abc.txt” Text File again, we can append the error of a command in it. To view the Text inside the “abc.txt” Text File, we used the “cat” command again and the Terminal displayed the Text inside the “abc.txt” File:

Now, to append an error in the text file, in our case, we will run the “pgadmin” command. As pgadmin is not installed on our Ubuntu System, the Terminal will display an Error. To save by Appending the error in a text file without showing it on the Terminal, the “2>>” will be used:

pgadmin 2>> abc.txt

The Cursor will move to the next line without displaying an error on the Terminal:

The already added text will now contain the “pgadmin” error text as well. To display the text inside the file, we used the “cat” command again:

Approach 5: Saving the Command Output Using the “&>” Command

The “&>” Command can be used in two approaches where the Output of a Command can be saved as well as the Error of any Command.

Saving the Error Output to a File using the “&>” Command

The Redirect can also be used to save the Error of a command by Overwriting the Already Added Text in a Text File. In our case, using the “abc.txt” Text File again, we can overwrite the content or text inside the “abc.txt” file. To view the Text inside the “abc.txt” Text File, we used the “cat” command again and the Terminal displayed the Text inside the “abc.txt” File:

Now, to Overwrite the existing file with the “&>” command, we will use the “firefox-dev” command:

firefox-dev &> abc.txt

As “firefox-dev” is not installed on our Ubuntu System, the Terminal is supposed to display an error but as we are using the “&>” command, the error will be saved to the file abc.txt:

Running the “cat” command again confirms that the saved Output from the Terminal has overwritten the File “abc.txt”:

Saving the Output to a File using the “&>” Command

You can also use the “&>” redirect to save the Output of a Command that runs in the Terminal without displaying any error. The Redirect “&>” can also save the Output of an executed command by Overwriting the Already Added Text in a Text File. In our case, using the “abc.txt” Text File again, we can overwrite the content or text inside the “abc.txt” file. To view the Text inside the Text File, we used the “cat” command again and the Terminal displayed the Text inside the “abc.txt” File:

Now, to overwrite the File “abc.txt”, the echo command is used and the output of the echo command will be saved to the “abc.txt” file using the “&>” redirect

echo "LinuxWays is the Best" &> abc.txt

The command will execute without any error, and the output will overwrite the “abc.txt” file:

To verify, we used the “cat” command again:

Approach 6: Saving the Command Output Using the “&>>” Command

The “&>>” Command can also be used in two approaches with one approach saving an executed output and the other approach saving the Error Output of a Command in a Text File.

Saving the Error Output to a File using the “&>>” Command

The Redirect can also be used to save the Error of a command by Appending the output with the Already Added Text in a Text File. In our case, using the “abc.txt” Text File again, we can append the output with the already added content or text inside the “abc.txt” file. To view the Text inside the Text File, we used the “cat” command again and the Terminal displayed the Text inside the “abc.txt” File:

Now, to append the Error output of the command, we used the “firefox-dev” command:

firefox-dev &>> abc.txt

As the “firefox-dev” package is not installed on our Ubuntu System, the command will display an error, and that error will be saved or appended in the “abc.txt” file instead of showing the error on the Terminal. The cursor moves to the next line without listing the error in the Terminal:

The error is appended in the “abc.txt” file. To verify, the “cat” command is used again:

Saving the Output to a File using the “&>>” Command

The “&>>” redirect can also be used to save the Output of a Command that runs in the Terminal without displaying any error. The Redirect “&>>” will save the Output of an executed command by Appending the output with the Already Added Text in a Text File. In our case, using the “abc.txt” Text File again, we can append the content or text inside the “abc.txt” file. To view the Text inside our previously used “abc.txt” Text File, we used the “cat” command again and the Terminal displayed the Text inside the “abc.txt” File:

Now, to Append the Text inside the Text file, we used the “echo” command:

echo "Learn more about Ubuntu on LinuxWays" &>> abc.txt

The output will not be displayed on the terminal rather it will append the output in the “abc.txt” file and the cursor will move to the next line:

To verify the Appended Text in the “abc.txt” file, we have used the “cat” command. You can see that the output is appended with the previously added text:

Method 2: Save the Terminal Output to a File Using the “tee” Command

The “tee” is another useful command for saving the Output of the Terminal in a File. Like the Redirect Method, the “tee” command can also be used in two approaches, where you can Overwrite an existing File in one approach, and in the other approach, you can Append the output to a Text File. Both approaches to saving the output to a text file are discussed below.

Approach 1: Using “tee” Overwrite an Existing File by saving the Terminal Output in the File

The “tee” command like the redirect can Overwrite an existing Text File by saving the output of the Terminal to that Text File. The basic syntax of the “tee” command in the Terminal is:

[command] | tee [fileName]

In our case, to use the “tee” command, we will use the “abc.txt” file. To list the text inside our previously used “abc.txt” file, we used the “cat” command:

Now, to Overwrite the “abc.txt” file, we used the “echo” command along with the String:

echo "LinuxWays is the Best How-tos Website" | tee abc.txt

This will display the output in the Terminal as well and Overwrite the file “abc.txt” with the output:

To verify, use the “cat” command again to list the text or content inside the abc.txt file. As you can see, the Text File “abc.txt” is overwritten with the new text:

You can also Overwrite the Existing Text File with the Terminal Error using the “tee” command. Note that with the “tee” command, if the Terminal responds with an error, the Error will only be displayed in the Terminal but it will not be saved in the Text File, although it will overwrite the Text File. The basic syntax to Overwrite the Existing File with the Output Error using the “tee” command is:

[command] | tee [fileName]

In our case, we used the “abc.txt” file again and listed the text inside the text file using the “cat” command:

cat abc.txt

The Already Added text inside the “abc.txt” file will be displayed in the Terminal:

Now, to run a command that displays the Error, we used the “firefox-dev” command as the “firefox-dev” packages are not installed on our Ubuntu System:

firefox-dev | tee abc.txt

The Terminal will display an error as the “firefox-dev” is not installed on our system and the error output will be displayed in the Terminal, but it will not be saved in the “abc.txt” file. Instead, the already written text in the abc.txt file will be removed. As you can see, the Terminal displays the Error:

Now to verify the “abc.txt” file, we used the “cat” command again and you can see that the Output is not written in the Text File, rather it overwrites the whole “abc.txt” file:

Approach 2: Using “tee -a” Append an Existing File by saving the Terminal Output in the File

The “tee” command can also Append the Output to an existing Text File by saving the output of the Terminal to that Text File. The basic syntax of the “tee” command to Append the Output in the Terminal is:

[command] | tee -a [fileName]

In our case, we will be using the “abc.txt” file again:

Now, to Append the Output of the Terminal in our “abc.txt” file using the “tee” command, we will use the “echo” command:

echo "The LinuxWays Guides provide you quality content with various to-dos" | tee -a abc.txt

This will Append the text to the “abc.txt” file and will display the Appended text in the Terminal as well:

To verify the “abc.txt” command, we have used the “cat” command. As you can see, the Text Appends to the already added Text:

If you want to Append the Existing Text File with the Terminal Error using the “tee” command, note that with the “tee -a” command, if the Terminal responds with an error, the Error will only be displayed in the Terminal but it will not be saved in the Text File. The basic syntax to Append the Existing File with the Output Error using the “tee” command is:

[command] | tee -a [fileName]

In our case, we will be using the “abc.txt” file again. To list the content inside the “abc.txt” file, we used the “cat” command again:

Now, to run a command in our Terminal that displays an error, we used the “firefox-dev” command in our case as it is not installed on our Ubuntu System:

firefox-dev | tee -a abc.txt

The error output will be displayed in the Terminal and will append the “abc.txt” file, but it will not be saved in the “abc.txt” file. Only the already written text in the “abc.txt” file will be available:

We will use the “cat” again command to verify the changes in our “abc.txt” file:

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

The “script” is another useful command that saves the Output of the Terminal in a Text File. The script command starts recording the changes made in the Terminal and once the recording of the script is stopped, the changes made in the Terminal are saved in the Text File. The basic syntax to use the “script” command is:

script [fileName]

In our case, using the “abc.txt” file again, we will list the Text inside the “abc.txt” file using the “cat” command:

Now, to start the script, we will use the script command along with the file name “abc.txt”:

script abc.txt

This will start recording the script in the Terminal and will Overwrite the “abc.txt” Text File with all the commands that are executed after the script is started:

Now, we will run the commands below one by one in the Terminal:

echo "hello from Linux ways"
firefox-dev
echo "bye"

As you will run the commands, the output of these commands will also be displayed in the Terminal:

To stop the script, we will simply use the “exit” command:

exit

This will stop the script right away and will show the “Script done” message inside the Terminal:

To verify the “abc.txt” command, the “cat” command will be used again:

cat abc.txt

As you can see, the changes made during the script are all listed or added in the “abc.txt” file:

Conclusion

It is crucial sometimes to keep track of the commands you execute in the Terminal that can be used for later use, analysis, or even review. With the “redirect” command, the “tee” command, and the “script” command, you can save the output of the Terminal in a Text File. All these commands with various use cases are discussed in this article.

Similar Posts