Table of Contents
This updated guide examines How to Use the Tee Command in Linux and organizes the essential facts, background, and practical takeaways in clear American English.
What does Tee hold?
A command likelswill display the contents of the current directory. In other words, it displays these content into a stdout (standard output), usually your screen, or more accurately a virtual terminal screen.
A command likels > file123will not display anything on the screen. That's because the>sign redirects all output to a file instead of displaying it as stdout. Now file123 will be filled with the contents shown earlier on the screen.
To display the contents of the folder on the screen and write it to a file, you must use two commands. But with atee, you can do both at the same time.
ls | tee file123
Why should Tee be used when you can run a similar command twice?
In the above example, it is clear that you don't need ateeif you can executelsnormally, then redo and redirect the output to a file. However, you will encounter situations in which the output is unique. Imagine the situation you are trying to diagnose a problem. You run:
diagnose | tee error.log
The errors you get may be unique. You want them to be displayed on the screen so you can see what's going on when checking everything. But you also want those errors to be saved in a file, to review later or paste the output into a discussion forum and consult people about it.
Another common situation that you may need toteeis when you want to take the output of the command to a location that only root users can read or write. The following command does not work:
/sbin/blkid > /root/somefile
Then you might think, just usesudo! But you will be surprised when this command is not working either:
sudo blkid > /root/somefile
That's because aftersudo blkidexecutes, you are still logged in as not a root user. And your shell (usually bash) tries to write to / root / somefile with your regular user information. To solve this, you can use thetee:
/sbin/blkid | sudo tee /root/somefile
Append text and redirect errors
Teeis a simple but useful command. Acommand | tee somefilebasiccommand | tee somefileusually sufficient for most situations. However, there are 2 cases where you will need these tips.
The first thing to know is atee, by default, always overwrites a file. If you run:
ls | tee somefile
Then run:
ls /tmp | tee somefile
The second command will overwrite the content of somefile and you will only see the contents of the last command executed. To change this behavior, you can create text that linksteeinstead of overwriting. To do so, simply use the -a switch command.
ls | tee -a somefile
The second thing to know is that not all outputs are the same. Error messages are handled differently and although they appear on the screen, they are not considered stdout, but are considered stderr and not handled bytee. The following is an example ofgrep.
grep -r L2TP /etc | tee somefile
The results shown will look like the following image:

Notice Permission denied is written into stderr. The only thing written into stdout is the highlighted text. That's why you notice that the content of 'somefile' is what is shown in the image below:

In this case,grepis used to search for text and is useful when error messages are not redirected to the file. Error messages only fill the file with unnecessary information. You just want to see the results found. But when you need an error message, use2>&1, to redirect stderr to stdout.
grep -r L2TP /etc 2>&1 | tee somefile
With this command, you will notice that somefile now also contains error messages.
Hopefully, this guide includes everything you need to make the most of thetee. But if you encounter some difficult situation with atee, leave comments in the comment section below for everyone to share.
Wish you use the successfulteecommand!
FAQ
What is How to Use the Tee Command in Linux about?
It provides a structured overview of Linux, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.