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.
Saving Command Output to A File
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:
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
When I access this file through the file manager, it looks like this:
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
The following file that once contained only my system information now also contains the output of my ls command:
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.
$ echo "print this text to my file" | tee /home/sana/myfile.txt
These are the contents of the file generated through the 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:
$ echo "here is some more text for printing" | tee -a /home/sana/myfile.txt
This is what the file looks like now:
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!