The Tee Command in Linux
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.txtA 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.txtWhy 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.
- Your shell sees this line and splits it into two parts (see the screenshot above).
- The first thing the shell does is open up the file for writing. It does this in your current shell (non-privileged).
- Since the shell is non-privileged, the file cannot be written to.
- 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.txtsudo 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.









