How to Save the Terminal Output to a File in Linux

Terminal Back

Oftentimes, while working on the Linux terminal, you might want to save the terminal output of a command to a file. This file may be used as information for another operation or to simply log terminal activity. Here are four different ways in which terminal contents can be saved in a file.

The following methods are applicable across all Linux distributions and can even be used on Macs and BSD. The only thing you have to think about is which method is best to get the job done.

1. Using Redirection Operators

The most common and basic way to redirect output from the terminal into a file is to use the > and >> operators. They direct only the output to a specified text file. They don’t redirect errors. Those will still appear in the terminal.

The > operator redirects output to a file and overwrites the contents of that file while the >> operator appends the output to the end of the file. Here’s an illustration:

First create a file using the touch command.

Terminal Output 1

A line is now echoed to “file1.txt” using the > operator. One can think of it as dumping the output of the echo command to “file1.txt.”

Terminal Output 2

Another line is echoed into “file1.txt.”

Terminal Output 3

On viewing the contents of “file1.txt,” we see that only the second line is stored in the file. This is because the > operator overwrites existing file content.

Terminal Output 4

If we wish to “append” content to the file, then the >> operator needs to be specified.

Terminal Output 5

We can view the result of the successful append operation.

Terminal Output 6

If you only want to save the errors, use the 2> and 2>> operators instead.

If you’re interested in logging everything, use &> and &>> to redirect all output (including errors) to a file without showing anything in the terminal.

2. Using tee command

With the redirection operators shown above, the output of the command is not displayed on the screen. If you wish for it to display the output on the terminal and write to a file, you can use the tee command.

The pipe (|) symbol passes the output of <command> as input to tee, which in turn displays the output on the screen. If the -a switch is specified, then tee appends that output to the specified file, else it would overwrite that file’s contents.

Terminal Output 7

According to the command demonstrated above, the text “fourth statement” should have been appended to “file1.txt.” Verification using the cat command corroborates our guess.

Terminal Output 8

You can also make use of the |& operator and the tee command to display everything and log it, too.

3. Using script command

With the script command, output of the commands typed following it would be automatically written to a file until prompted to cease. This can be likened to a session which records terminal activity.

First, the script command is invoked with the name of the file to store terminal activity.

Terminal Output 9

A message prompts that the script has started, then commands are typed one after the other – here date, pwd, ls and cal.

Terminal Output 10

To terminate the scripting, the exit command is invoked. There is a message that the “script” operation is done. Viewing the contents of “script_log.txt” using cat, we see that the file’s contents look like an exact replica of the terminal.

Terminal Output 11

Towards the end we can see the timestamp when the script was completed. When this file is viewed in a text editor, we can see some text content and junk, which is actually the bash prompt in a format understandable by bash shell.

Terminal 29

Only when this file is viewed in the shell using the cat command, we get fully intelligible information.

Let’s try another one, saving the terminal output in “2-script_log.txt.”

Terminal Output 12

Commandspwd and ls are typed.

Terminal Output 13

Without typing exit, the terminal window is closed. When we attempt to view the contents of “2-script_log.txt,” we can see that it is empty.

Terminal Output 14

When a script session has been initiated, the contents of that session are retained in memory and written to the file only when the exit command is invoked. Here, since exit was not invoked, that session’s contents were not saved to “2-script_log.txt.”

The overwrite and append behavior of script is similar to tee and the redirection operators. The -a switch appends the contents of a session to a previously existing file. Here, output of the echo command is appended to “script_log.txt.”

Terminal Output 15

Let us view the output of “script_log.txt.” We can see the previously saved output of the date, pwd, ls, cal commands followed by the timestamp; afterwhich, we find the next session’s information ending with the timestamp.

Terminal Output 16

4. Using logsave command

logsave behaves similar to tee – it displays the output on the screen and also saves it to a file. It is used as shown below:

Terminal Output 17

logsave writes the output of <command> to the file specified by <file_name>. Let us view the output of “mylog_file.txt.”

Terminal Output 18

There is a good deal of information stored along with the result of one command. Two timestamps are stored here: the first is the time at which the command was initiated, and the second is the time at which the command completed its execution. Here, the timestamps are the same. Although, when a recursive directory listing is initiated in the “/home” directory on a multi-user system, the command would take a while to fully execute. In that case, the start and end timestamps would be different.

Output of more commands can be appended to the same file when the -a switch is used with logsave.

Terminal Output 19

When we attempt to view the contents of “mylog_file.txt,” we see that the output of the two previously entered commands are demarcated by a line.

Terminal Output 20

The above are some of the more useful ways to save a terminal output to a file in Linux. Which one do you prefer?

Related:

One comment

  1. Nice article! I did not know about the ‘script’ cmd. But there is no need to use ‘touch’ to create files. In fact, touch is not for creating files. It is extremely slow and should be avoided at all! To create file, simply use redirection:
    $ >myfile.txt
    Or
    $ >myfile.txt <<<"Here is the heading text of the new file"

    Redirecting is the first thing the shell does when reading a new typed cmd..

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.