Save command output to file on Linux

We all know how running a command in the Linux command line, the Terminal, results in the execution of the command and printing of the results in the Terminal itself. Sometimes, this immediate display of output is not enough, especially if we want to save the output for later use. Fortunately, the Linux bash and all operating system bash, for that matter, is equipped with the ability to print the output of a command to a specified file. In this article, we will deal with the following two tasks:

  • Saving Command Output to A File
  • Printing Output on Terminal and Saving it to a File

The commands mentioned in this article have been executed on an Ubuntu 18.04 LTS system.

Saving Command Output to A File

We will run a few examples where the output of the commands will be saved on the file name we specify.

Create New File/Replace existing file

If you want to save the output of a command to a new file or replace the contents of an already existing file with the output of the command, please use the following syntax:

$ [command] > [/filelocation/filename]

For example, I will use the lscpu command(that displays system information) and then print its contents to the file named systeminformation.txt. This file does not already exist on my system.

Redirect command output into a file

When I access this file through the file manager, it looks like this:

Content of file shown in editor

Append Output to Existing File

If you want to save the output of a Linux command to a file without messing with its already existing contents, you can use the following syntax:

$ [command] >> [/filelocation/filename]

For example, the following command will append the result of the ls command at the end of my already existing file systeminformation.txt.

Redirect command output to a file and append the content

The following file that once contained only my system information now also contains the output of my ls command:

The result

Printing Output on Terminal and Saving it to a File

You might have noticed that the commands that we mentioned above only save the output to the file without printing them on the Terminal itself. Use the following syntax if you want to view the output of the command on the Terminal as well:

$ [command] | tee [/filelocation/filename]

For example, the text that we want to echo in the following image will now be echoed on the Terminal and also printed to the file myfile.txt.

Use the tee command

These are the contents of the file generated through the command:

Output printed into a file with tee command

If you want to append the output of a command to an already existing file. Please follow this syntax:

$ [command] | tee -a [/filelocation/filename]

For example, the following image shows how some more text will be echoed and then added to my already existing file:

Using the append option of tee command

This is how the file looks like now:

appending text with tee command

These output oriented text files that we generated through this article can be much more useful in some cases than the usual printing of output on the terminal. That’s how powerful the Ubuntu bash is!

How to Save the Output of Command in a File using Linux Terminal