Sitemap
Lumenyx

Lumenyx

The Tee Command in Linux

2 min readJul 20, 2026
Press enter or click to view image in full size

Tee is a command that is used to read from standard input and also write to standard output and files.

It writes to a file, and it also outputs that same content onto the screen.

Why is this command used?

Imagine this scenario:

You’re trying to echo something into a write-protected file. You can’t do it because you didn’t use sudo. This is expected.

echo "Test" > root-owned-file.txt
Press enter or click to view image in full size

A beginner would assume that adding sudo to the echo command would resolve this, however this also doesn’t work:

sudo echo "Test" > root-owned-file.txt
Press enter or click to view image in full size

Why doesn’t this work?

Get Lumenyx’s stories in your inbox

Join Medium for free to get updates from this writer.

This doesn’t work because the redirection (the >) is handled by your CURRENT non-privileged shell.

The echo command was privileged, but the act of opening up the file was not.

Press enter or click to view image in full size
  1. Your shell sees this line and splits it into two parts (see the screenshot above).
  2. The first thing the shell does is open up the file for writing. It does this in your current shell (non-privileged).
  3. Since the shell is non-privileged, the file cannot be written to.
  4. The sudo echo "Test" part doesn’t even get run.

We can use the sudo tee command to get around this:

echo "Test1" | sudo tee root-owned-file.txt
Press enter or click to view image in full size

sudo tee avoids this as it doesn’t need shell redirection (>), the echo command is instead piped (|) into sudo tee, which then writes to your file as root.

--

--