How To'sLinux

2 Ways To Save Terminal Output of a Command in Linux

When you run a script or command it generates an output on a terminal for viewing. It can be stored in a file, sometimes it can be useful because you won’t need to copy/paste from the terminal.

#1 Using Redirection To Save Command Output To A File in Linux

We can use a redirection operator to save script OR command output to a file.

  1. “>” Operator redirects command output and replaces existing content on a file.
  2. “>>” Operate add command output at the end of the existing content in the file.

ping -c 3 > output.txt

It will generate an output.txt file automatically and store the output of the command. If we use the similar command again it will replace the content.

Let’s use the redirection operator in append mode “>>”

ping -c 3 >> output.txt

This double redirection operator “>>” would not replace your output instead it will append the output to the existing content, which can be sometimes helpful.

To avoid output errors you can use postfix 2>&1:

ping -c 3 >> output.txt 2>&1

#2 Using tee Command To Display Output And Save It

Now, we’re going to use tee command to display the output on the terminal and save the output this would do both, unlike the redirection operator which we used previously.

Let’s use tee command:

nslookup | tee output.txt

The file would be created if doesn’t exist already.

These simple commands can be useful in different scenarios, can be used to save the important output while running the commands instead of spending time savings output. Especially for Pen-testers and Linux devs where they need to save important outputs to a file.

Noor Qureshi

Experienced Founder with a demonstrated history of working in the computer software industry. Skilled in Network Security and Information Security.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button