How to save command output to a file using Linux terminal

We all know how executing a command on the Linux command line, Terminal, results in the command being executed and printing the results in Terminal itself. Sometimes this immediate display of output is not enough, especially if we want to save the output for future use. Fortunately, Linux Bash and all Bash operating systems, for that matter, are equipped with the ability to print command output to a specified file. In this article, we will consider the following two tasks:

  • Saving command output to a file
  • Printing output on the terminal and saving it to a file

The commands mentioned in this article were executed on the Ubuntu 18.04 LTS system.

Saving command output to a file

We will run several examples in which the output of the commands will be saved in the file name that we specified.

Create new file / replace existing file

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

$ [command] > [/filelocation/filename]

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

Redirect command output to file

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

The contents of the file are displayed in the editor.

Add output to existing file

If you want to save the output of the Linux command to a file without linking to existing content, you can use the following syntax:

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

For example, the following command will add the result of the ls command to the end of my existing systeminformation.txt file.

Redirect command output to file and add content

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

Result

Printing output on the terminal and saving it to a file

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

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

For example, the text that we want to display in the next image will now be displayed in the terminal, as well as printed in the myfile.txt file.

Use tee command

This is the contents of a file created using the command:

The output is printed to the file with the tee command.

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

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

For example, the following figure shows how another text will be displayed and added to my existing file:

Using the tee Add Option

Here’s what the file looks like now:

add text using tee command

These output-oriented text files that we created in this article can be much more useful in some cases than printing output on a terminal. This is how powerful Ubuntu Bash!

How to save command output to a file using Linux terminal

Sidebar