Store the output of a Linux command to a file

Usually, while working on a Linux terminal, you may want to save the output of a command to a file. Here are 4 different ways in which to save terminal content in a file.

This file can be used as information for another operation or to log terminal activity. Here are 4 different ways in which to save terminal content in a file.

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

1. Use the redirection operator

The most basic and common way to redirect output from the terminal to a file is to use the > and >> operators. They redirect output to a specified text file.

The > operator redirects the output to a file and overwrites the contents of that file while the >> operator appends the output to the end of the file. Here is an illustrative example:

First create a file using the touch command.

Now, a line is inserted into 'file1.txt' using the > operator. This can be thought of as dumping the output of the echo command into 'file1.txt'.

echo "first statement" > file1.txt

Another line is included in 'file1.txt'.

echo "second statement" > file1.txt

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

If you want to append content to the file, the >> operator needs to be specified.

If you just want to save the error, use the 2> and 2>> operators instead.

If you care about logging everything, use &> and &>> to redirect all output (including errors) to a file without displaying anything in the terminal.

2. Use the tee command

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

| tee -a

Icon | pass the output of the command as input to tee, thereby displaying the output on the screen. If switch -a is specified, then tee will append that output to the specified file, otherwise it will overwrite the contents of that file.

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

|& tee -a

3. Using the script command

With the script command, the output of commands entered after it is automatically written to a file until prompted to stop. This can be likened to a session that records terminal activity.

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

script

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

Store the output of a Linux command to a file Picture 1Store the output of a Linux command to a file Picture 1

To terminate, the exit command is called. There is a message that the 'script' operation has completed. View the contents of 'script_log.txt' using cat. It can be seen that the contents of the file are like an exact copy of the terminal.

Store the output of a Linux command to a file Picture 2Store the output of a Linux command to a file Picture 2

But only when this file is viewed in the shell with the cat command, will we get completely understandable information. So try another alternative, save the terminal output in '2-script_log.txt'.

Commandspwd and ls are entered.

Without typing exit, the terminal window is closed. When viewing the contents of '2-script_log.txt', you will see it as empty.

When a script session is started, the contents of that session are kept in memory and written to the file only when the exit command is called. Here, since exit is not called, the contents of that session are not saved to '2-script_log.txt'.

The script's overriding and appending behavior is similar to the redirect operators and the tee command. Switch -a appends the session's contents to the previously existing file. Here, the output of the echo command is appended to 'script_log.txt'.

You can see the previously saved output of the date, pwd, ls, cal commands, followed by timestamp; then information about the next session (ending with timestamp).

4. Use the logave command

logave works similar to tee - it displays the output on the screen and also saves to a file. It is used as below:

logsave

logave writes the output of the commandto the file specified by . Take a look at the output of 'mylog_file.txt'.

There is a lot of information stored with the result of a command. Two timestamps are stored here: The first is when the command was started, and the second is the time at which the command completes execution.

The output of multiple commands can be added to the same file when switch -a is used with logave.

logsave -a

When viewing the contents of mylog_file.txt, you will see that the output of the two previously entered commands is delimited by a single line.

4 ★ | 1 Vote