How to save the command output in Linux Terminal?

As you see whenever you run command on linux terminal ,it show the output on screen to know what changes happens.

If I tell you there is a way to save the output into text, In your mind, you are just thinking it easy just copy & paste to the text file that’s it. Why read this article then?

After reading this you will get to know the automatic way to store output into text-file, sssh don’t tell anyone about this.

If you are searching something like command output should automatically get saved to file.

How to use redirection to save command output in Linux?

You can leverage the redirection command to save the command output from the terminal.

There are combination of redirection symbol which you can use like “>” ,“>>”,“&>”,”&>>

This redirection “>”, “>>” is used to redirect output or stdout into text file.

  • A “>” you can use this redirects the command output to a file, If the content already exists then it will overwrite.
  • A “>” is used to redirects the command output to a specific, If the content already exists then the output will append.

This redirection “&>”, “&>>” is used to redirect error output or stderr into a text file.

  • A “&>” you can use this redirects the command output & error output to a file, If the content already exists then it will overwrite.
  • A “&>>” is used to redirects the command output & error output to a specific, If the content already exists then the output will append.

In case if the output file does not exist in a specific directory then it will automatically create and save the file.

Make sure which redirect you are using,If you have used “>” then the past data will get replace with a new file,If you need both output and error output then use “&>”.

To get into more detail we will use this command to demonstrate the use of redirect.

We have use lsblk command to show list of partition with redirect “>” to save output into trendoceans.txt

How to use “>” , “>” to redirect the output into a text file?

Syntax

command > filename.txt
 save the command output from the Linux terminal
Use of “>”to save output

If I use the same text file with this “>” redirect data will get replaced with the new output.

Now an example of “>>” this redirect will make sure that the last save file should not get a wipe and append new output into the same file.

Syntax

command >> filename.txt
 save the command output from the Linux terminal using >>
Use of “>>”to save output

As you can see the output first I have used lsblk command with “>” and now hostnamectl with “>>”.

How to use “&>” , “&>>” to redirect the output into a text file?

Above We have use lsblk and hostnamectl command to show list of partition with redirect “>” ,”>” to save output into trendoceans.txt.

When we use “>”,”>” then it will save Output only or stdout, this will not save error output or stderr.

To resolve this we can use “&>”,”&>>” to save both output and error output

To get into more detail we will use this command to demonstrate the use of redirect.

Syntax

command &> filename.txt
 save the command output from the Linux terminal and Use of "&>"to save error output
Use of “&>”to save error output

We have typed the wrong lsblk command to save error output or stderr into a specific text file.

In this also, If we use “&>” then the last save error output will get wipe with a new one. So, I’m not using this to demonstrate.

Syntax

command &>> filename.txt
Use of "&>>"to save error and standard output
Use of “&>>”to save error and standard output

In this above image you can see i have type correct command with “&>>” to save command output.

When i pass the cat command to check output you can see this “&>> or “&>” save both error and standard output.

This way, you can redirect command output to the file even you can use tee command to redirect output to the terminal.

If you want to share something please comment down

Leave a Reply