How to save the output of a terminal command

Saved command output

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.

Save the output of a command from the File menu

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.
upower
Related article:
upower, a simple command to check the battery status of our laptop

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:

Command output: Neofetch

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?

about alternatives to ls command
Related article:
Alternatives to the 'ls' command to list the contents of a directory

The content of the article adheres to our principles of editorial ethics. To report an error click here!.

5 comments, leave yours

Leave a Comment

Your email address will not be published.

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   newbie said

    In Linux does not use ">" or ">>" as in Windows?

    1.    Diego said

      I have the same question, what is the difference between dumping the data with> or using the tee command?
      From what I have been able to read, the difference is that tee also shows it to you on the screen. That is> it dumps it to the text file without showing the output and tee returns it to a file and additionally shows it to you on the screen. Does anyone confirm me?

      1.    pablinux said

        Hello: yes, when the> is used we do not see anything, so it is actually a "half" output. tee shows it and saves it, which also helps us to make sure that we have written it correctly.

        A greeting.

  2.   Joseph said

    What they could explain is how a change to a file that we edit is saved, at the bottom of the console a task bar appears in two rows with things like View, Exit, search, Replace, Spelling, Paste and others
    But how do you do to Save, for example? On the left side of the word Save, there are two symbols «^ Ô» but if you type them they are written in the console and it is not saved ...
    How difficult was it for those who do this to put a save button?
    In Linux, those who do it, should think: Why make it easy if we can make it complicated

  3.   emerson said

    enter to see if I found out how to save the changes when I edit a file with nano,
    for example: sudo nano edit cd xxx,

    the file is opened, not every time, (that this is linux) and after you modify it, the console, so as not to lose that air of mystery that characterizes it, and to make things difficult for you, (that in easy linux there is no nothing, whatever the masochism fans say,) at the bottom, you see two lines with different options that explain what will happen if you execute the command that precedes it, these commands are something like this: ^ X, you think, If I give the popcorn «^» and the capital letter X, the command will work and the changes will be saved …… Well no, then you kgas in all the dead of those who make linux, and you wonder why you believe them when you they tell all the lies they tell about linux, but finally they convince you, (they) that the donkey is you, that your logic is abnormal and then you fall here, thinking that it will explain it to you, but no, it explains something different, that does not serve you either….
    Linux is like that