Save Output Of A Command From the Debian Terminal To A File Debian linux

We all know how running a command in the Debian command line, the Terminal, results in the execution of the command and printing of the results, if any, 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 work on the following two scenarios:

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

We have run the commands and procedures mentioned in this article on a Debian 11 Bullseye system.

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

In order to open the Terminal, access the Application Launcher search through the Super/Windows key and then search for Terminal as follows:

<img alt="Debian Terminal" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-327.png" ezimgfmt="rs rscb10 src ng ngcb10" height="213" loading="lazy" src="data:image/svg xml,” width=”229″>

Create New File/Replace an 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.

$ lscpu > /home/sana/systeminformation.txt

<img alt="Save command output to file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-328.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="39" loading="lazy" src="data:image/svg xml,” width=”584″>

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

<img alt="System information" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-329.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="588" loading="lazy" src="data:image/svg xml,” width=”905″>

Append Output to an already 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.

$ ls >> /home/sana/systeminformation.txt

<img alt="Append Data to file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-330.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="38" loading="lazy" src="data:image/svg xml,” width=”571″>

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

<img alt="Append command output to file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-331.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="568" loading="lazy" src="data:image/svg xml,” width=”896″>

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.

$ echo "print this text to my file" | tee /home/sana/myfile.txt

<img alt="Echo into file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-332.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="54" loading="lazy" src="data:image/svg xml,” width=”733″>

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

<img alt="Save output of echo command to file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-333.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="128" loading="lazy" src="data:image/svg xml,” width=”902″>

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:

$ echo "here is some more text for printing" | tee -a /home/sana/myfile.txt

<img alt="Use pipe to save command output to file" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-334.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="77" loading="lazy" src="data:image/svg xml,” width=”737″>

This is what the file looks like now:

<img alt="File content" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-335.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="107" loading="lazy" src="data:image/svg xml,” width=”907″>

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 Debian bash is!