Command Line

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as we’re going to save a command’s output to a file – while showing that same output in the terminal. This is something we’ve not quite done on this site before and something you might find interesting.

NOTE: This article assumes that you’re using Bash.

We’ve sort of covered redirect operators before. Read this article:

How To: Write Text To A File From The Terminal with “>” and “>>”

However, this time, you’re going to enter a command and see the output in the terminal, unlike what you’d see in the above-linked article. On top of that, you will simultaneously save that output to a file.

This can be handy to keep track of a command’s output over time. This can also be handy if you’re trying to audit a system and want to keep track of the output from the command.

For this article, we’re going to just use a simple example command. We’ll be making use of the uptime command because it’s easy and universal. If you’re using a desktop (or server) Linux, you have this command available.

How To: Find Your Uptime In Linux

This article is also going to make use of the tee command. If you’re a regular reader, you’ll know we’ve covered this command before. Then again, if you’re not a regular reader, you can just as easily learn about the tee command by reading the following article:

Mastering The Power Of Linux Tee Command

We’ll also be using the terminal, of course. We almost always use the terminal!

Save A Command’s Output To A File:

As suggested above, this is another article that relies on an open terminal. You can usually open a terminal by pressing CTRL + ALT + T on your keyboard. A terminal is otherwise available in your application menu.

With your terminal open, let’s just view your uptime with this command:

uptime

Next, we’re going to tell the command to show the output AND save the output to a file. That’s quite simple:

uptime 2>&1 | tee <file_name>.txt

So, let’s try this example command:

uptime 2>&1 | tee uptime.txt

Now, you can verify that this worked with this command:

cat uptime.txt

Every time you run that command, it will clear out the existing text and write the most recent output to the uptime.txt file.

If you’d rather append the data, that’s easily done. It looks like this:

uptime 2>&1 | tee -a <file_name>.txt

Again, as a handy example:

uptime 2>&1 | tee -a uptime.txt

If you run that command multiple times and then check it with cat uptime.txt you’ll see that the -a flag will append the output. So, each time you run this command it will add the new output to the file.

That’s all there is to it. This is a handy thing if you want to monitor the output of a command over a period of time. You can alias the uptime command to this command and have a record of all the times you ran the uptime command in the terminal.

I’m sure there’s more that you can do with it, but that’s a basic idea that you can take with you. It’s a pretty handy command and one that I recently shared via PM with a Linux.org user. Seeing as it was on my mind, I figured I’d make it an article. I’d call it a ‘short’ article but the title was already too darned long!

Closure:

Now you know how to save a command’s output to a file while also showing the command’s output in the terminal itself. This is a handy enough command and easy enough to do. It seemed like it’d make an easy article for folks, so I wrote it. I don’t have any other justification, though it was not all that taxing to write.

In my defense, I deserve an easy article now and then!

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

Recent Posts

Short: The Halt Command

Today's article is intentionally short because we're simply going to learn about the basics of…

52 mins ago

Installing Flatpaks In Linux

Today we're going to have a pretty easy article where you'll learn about installing Flatpaks…

2 days ago

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

4 days ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

6 days ago

View Detailed Hardware Information

There are many tools for showing your hardware information and today we'll get to view…

1 week ago

How To: Install Wine In Lubuntu

Today's article isn't going to be complicated or long as we are going to cover…

1 week ago