Sabtu, 21 November 2020

How to Save the Output of a Command to a File in Linux Terminal [Beginner’s Tip]

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 In Linux
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.



from It's FOSS https://ift.tt/3pIxYKq
via IFTTT

Tidak ada komentar:

Posting Komentar