22 C
Jaipur
Sunday, November 22, 2020

How to Save the Command Output to a File in Linux Terminal

Must read

How To Enable 2FA On Facebook Without A Phone Number?

At one point in time in order to set up 2FA (two-factor authentication), Facebook required you...

The Xtron Pro Is a Unique Game-Console cum Smartwatch

Over the past few years, we have seen many companies develop educative tools and robots with an aim to help kids learn to code....

Epic Games Store Now Shows the Prices of Games in INR

Apart from fighting the epic legal battle with Apple, Epic Games is also working on its online games Store to bring support for more...

How to get rid of multiple app pages using the new App Library in iOS 14

iOS14 is probably the biggest update in iOS history. The update brings several improvements to the operating system along with a few first-ever features...

When you run a command or script in the Linux terminal, it prints the output on the screen for your immediate viewing.

There will be times when you need to save the output to a file for future references. Now, you can surely copy and paste in Linux terminal but there are better ways to save the output of a shell script or command in Linux command line. Let me show them to you.

Method 1: Use redirection to save command output to file in Linux

You can use redirection in Linux for this purpose. With redirection operator, instead of showing the output on the screen, it goes to the provided file.

  • The > redirects the command output to a file replacing any existing content on the file.
  • The >> redirects adds the command output at the end of the existing content (if any) of the file.

Use the STDOUT redirection operator > for redirecting the output to a file like this:

command > file.txt

If the file.txt doesn’t exist, it will be created automatically. If you use the > redirect again with the same file, the file content is replaced by the new output.

The example below demonstrates it better. It first saves the output of ls -l command. And then later, it replaces the content of the file with the output of ls *.c command.

Redirecting command output to file

If you don’t want to lose the content of the existing file while saving the output of a script or command, use the redirection operation in append mode with >>.

command >> file.txt

This example demonstrates it better:

Redirecting Command Output To File
Redirecting command output to file in append mode

Even here if the file doesn’t exist, it is created automatically.

Bonus Tip: Save Linux command output as well as error to a file

If your Linux command returns an error, it doesn’t get saved in the file. You can save both the command output and command error in the same file using 2>&1 like this:

command > file.txt 2>&1

Basically, 0 stands for standard input, 1 for standard output and 2 for standard error. Here, you are redirecting (>) standard error (2) to same address (&) as standard output (1).

Method 2: Use tee command to display the output and save it to a file as well

By the way, did you notice that when you send the command output to a file, you cannot see it anymore on the display? The tee command in Linux solves this problem for you.

Like a tee pipe that sends water stream into two directions, the tee command send the output to the display as well as to a file (or as input to another command). You can use it like this:

command | tee file.txt

Again, the file will be created automatically, if it doesn’t exist already.

You may also use the tee command in append mode with option -a in this manner:

command | tee -a file.txt

Let me demonstrate it with some easy to follow examples:

Display And Save Linux Command Output

I have used simple Linux commands in my examples. But rest assured, you can use these methods to save the output of bash scripts as well.

Note: Avoid pipe pitfall while saving command output to file

You probably are familiar with pipe redirection. You may use it to combine Linux commands but you cannot pipe the output to a file. It will result in error that filename command not found:

Pipe Output To File Linux

This is because pipe redirects the output of one command to input of another command. And in this case, you give it a file name while it was expecting a command.

If you are new to Linux command line, I hope this quick tutorial added to your Linux knowledge a bit. I/O redirection is an essential concept that one should be aware of.

As always, questions and suggestions are always welcome.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How To Enable 2FA On Facebook Without A Phone Number?

At one point in time in order to set up 2FA (two-factor authentication), Facebook required you...

The Xtron Pro Is a Unique Game-Console cum Smartwatch

Over the past few years, we have seen many companies develop educative tools and robots with an aim to help kids learn to code....

Epic Games Store Now Shows the Prices of Games in INR

Apart from fighting the epic legal battle with Apple, Epic Games is also working on its online games Store to bring support for more...

How to get rid of multiple app pages using the new App Library in iOS 14

iOS14 is probably the biggest update in iOS history. The update brings several improvements to the operating system along with a few first-ever features...

Google’s New AI-System Helps Blind People Run Independently

During a one-day hackathon event at Google back in September 2019, Thomas Panek, the CEO of a non-profit organization for the blind asked Google...