Learn the tee Command in Linux: Redirect and Log Output Easily

The Linux Crash Course series on Learn Linux TV will teach you an important Linux-related skill or concept in each video. In this episode, Jay will cover the tee command, which lets you see and save output at the same time!

YouTube player

LCC – The tee Command

Hello and welcome back to Learn Linux TV! In this article, we’re going to take a look at the tee command. The tee command is very useful because it lets you print command output to the your screen and one or more files at the same time.

I pity the fool that doesn’t use this command.

This article is part of my Linux Crash Course series, now over 70 videos / articles long! Each episode / article covers one Linux-related topic, and they can be watched / read in any order. You’ll find this and all the others on the Linux Crash Course series playlist on this channel. Check it out, it’s a great way to Learn Linux.

And speaking of Learning Linux, I also wanted you to know that I have two brand new courses available…

…Thank you for your support! Now, let’s dive in and learn the tee command.

Basic Usage

So, let’s get started. And the best way to learn the tee command in my opinion is to see it in action.

Consider the ls command:

ls

It’s a very simple command, and one you might know about already. It lists the contents of your current working directory.

And, as you also may or may not know, you can redirect output to a file:

ls > file_list.txt

For most of you, what I’ve shown you so far isn’t new. But if it is, you’ve just seen an example of running the ls command, and another example of redirecting its output to a file.

But what does this have to do with the tee command?

Well, like I mentioned earlier it’s capable of printing output to the screen and a file at the same time. Let’s see the previous example with the tee command:

ls | tee file_list.txt

With this command, I ran the ls command just like before, but I redirected its output into the tee command. I still saw the output on the screen, and the same output is now in the file_list.txt file.

The tee command accepts a file name, and you can direct it to a local file like I just did or the full path to a file somewhere else.

Another easy example is this one:

tee --help

With this command, we can view the help page for the command, in case you need a refresher after reading this article.

So admittedly, the previous examples weren’t very useful. Let’s see another example that might be a bit more helpful.

dpkg --get-selections |tee packages.list

This particular command is limited to those of you that are running either Ubuntu or Debian, or perhaps a distribution based on one of those. If you’re not running a compatible distribution, don’t worry about it – you can simply take notes.

Anyway, what this command is going to do is print a list of every package installed on the system. This is great for auditing packages, or you can even import this list into a fresh installation to ensure the same packages are installed.

That’s beyond the scope of this article, but it really doesn’t matter what command you’re running when it comes to the tee command.

This command exported a list of packages to a file, but also printed the same information to the screen. This is great in case I also need to look through the output, with the tee command I won’t have to immediately open the file after.

Perhaps even more useful, while you update the packages on your server, you can also write the output to a file in case you need it for auditing purposes:

sudo apt dist-upgrade | tee update_results.txt

In this case, we were able to update the system, see the results on the screen, and also have a file to look at in case we need it later. And if you’re running a different distribution, you can replace the command at the beginning with whatever the appropriate update command is for your distro.

So now you’re probably seeing why this command can be helpful, and if you haven’t already figured it out, you can probably see why it’s called the “tee” command in the first place.

Another trick that you should know about, is the fact that you can write output to more than just one file at a time:

sudo apt dist-upgrade | tee /var/log/update_results.log /mnt/audit/reports/update_results.log

I’m not going to execute this command, because it’s hypothetical anyway. But in this case, we’re able to see the update process play out on our screen, and then save the same output to a file in two different directories.

The tee command is literally that simple. When it comes to basic usage, that’s all there is to it. But before I close out this article, there’s a few more things you should know about.

First, when using sudo, how you go about using the tee command changes a bit. Consider the previous command:

sudo apt dist-upgrade | tee /var/log/update_results.log /mnt/audit/reports/update_results.log

That command is great if my user has access to the files I’m writing to, but if I don’t, I’ll get a permission denied error.

And this is despite the fact that we were already using sudo at the beginning of the command.

The reason why this might fail is because I’m only using sudo with the first command, not the second. So I’m using sudo with apt, but not sudo with tee.

sudo apt dist-upgrade | sudo tee /var/log/update_results.log /mnt/audit/reports/update_results.log

So in this case, we’ll need to make sure we have permission to access the file we want to write the output to. If we don’t, we can use sudo with the tee command.

Up until now, every time we’ve used the tee command, it’s always been overwriting the file every time. It’s quite possibly the case that you’ll want to retain previous information that used to be in the file, along with any new output. Let’s see how we can do that.

sudo apt dist-upgrade | tee -a update_results.log

All I did was add the -a option, which activates append mode. With append mode, the file will not be replaced. Instead, tee will append any new information to the end of the file, if it’s present.

  • Ad-free Content
  • Early access to select videos
  • Discord access

And more!