We are in 2019 and there are still many who do not use Linux because they believe that everything is done through a command line. This is not true, and Ubunlog readers know it well. What is certain is that the Ubuntu terminal (or the Bash) is a very powerful tool, so much so that Microsoft has included it in Windows 10. Sometimes, we want to share the output of a command for, for example, other users to help us with a problem and in this article we will show you how to save that output.
There are many ways to save the output of a terminal command. In this article we will show you two very simple ones and another one a little more «linuxera». The simplest and most accurate of all is the one I have used to save the information it shows neofetch as you can see in the image that heads this article. The problem is or may be that not all console / terminal applications have such an option. We tell you everything after the cut.
Table of Contents
The simplest option I am talking about is the same as the one we use to save a text file from any application of this type. It's about going to Menu file and look for something like "Save." In Konsole, the Kubuntu terminal app, it says "Save output as ...". Things to know:
- The output is perfect. This means that it will copy exactly what is in the terminal. In the case of Neofetch and as you can see in the screenshot, it takes everything the same, but in plain text. It does not respect colors, which can come in handy as we will explain later.
- Copy ALL what's in the terminal. This can be a problem if we only want to share a part of what we have. To avoid copying a lot if we have been using the terminal before saving what we want, we can use the "clear" command to clear the screen.
- El file is saved with TXT extension, although some apps may also offer the ability to save it as HTML.
Copying and pasting
This is simpler than the previous one, right? The terminal allows let's copy and paste text, but there are a few things to keep in mind:
- If the output is very long, selecting all the text can be tedious. I would recommend it only on short outings.
- Logically, what was copied has to be pasted somewhere, be it a text document created by hand, a Tweet, an e-mail, etc.
- Can't use keyboard shortcuts. If we press Ctrl + C, which is the keyboard to copy, we will enter ^ C in the terminal. It goes without saying that pasting doesn't work either and what we would introduce in the terminal would be ^ V.
Saving the output of a command, "linuxera" version
In the "linuxera" version we will do everything from the terminal. But before explaining how it is done, we also have to explain that:
- Doesn't work with all commands. When having to add an extra option (-h), some commands will give us an error.
- The good: just save what we ask.
- Not all those that work come out perfect. For one of my tests I have saved the Neofetch information and, as I have explained before, the one that is saved in plain text without colors was a good option because using the terminal it will add characters that try to define the color to the existing characters, being that way:
Among the commands that do work we have "df", so we will use that command for the test. The line would be like this:
df -h | tee test.txt
From the above command:
- df It is the command that will show us the used space of our disks.
- The option -h it will ensure that the output will be easy for a human to read.
- tee will be the command that will save it.
- test.txt is the output file. If we do not indicate the path, it will save it in our personal folder. It also works in .log extension, for example.
If we want to open the newly created file from the terminal, we can do it with the command «cat test.txt», as long as we have saved the file with that name and we have it in our personal folder.
As we have mentioned, the terminal is more powerful than simple tools and from it we can add more information to the output file. For this we will use the option -a (add or add) in front of the file, such as
df -h | tee -a test.txt
With the previous command we would add the new storage information for our disks to the test.txt file.
Do you already know how to save the output of a command in Linux?