In the Linux terminal, redirecting the output of a command into a text file is a fundamental skill. Using the standard redirection bracket (>), you can take the results of a massive directory scan or a log file analysis and save it to a document. However, standard redirection has a major flaw: it is “silent.” When you redirect output to a file, you cannot see that output on your screen at the same time.
If you are running a command that takes 20 minutes to complete, you want to monitor its progress on your monitor while simultaneously saving a permanent record to a file. To achieve this, Linux administrators use the tee command.
Understanding the Concept of tee
The command is named after a “T-splitter” used in plumbing. Just as a physical T-pipe splits a stream of water in two directions, the tee command takes the output of your program and splits it. It sends one stream of text to your terminal screen so you can read it, and simultaneously writes a second stream of text into a file.
Basic Syntax and Usage
The tee command is almost always used in conjunction with a “pipe” (|). You run your primary command, pipe the output into tee, and specify the filename.
[command] | tee [filename]
For example, if you want to ping a remote server to test network stability, and you want to watch the pings on your screen while saving a log of the results, you would type:
ping 8.8.8.8 | tee network_test.log
As the pings execute, they will scroll down your terminal window normally. When you eventually press Ctrl+C to stop the command, you can use the cat command to view network_test.log, and you will find an exact, permanent copy of the output.
Appending to an Existing File
By default, the tee command behaves like the single redirection bracket (>). If the file you specify already exists, tee will completely overwrite the old file with the new data.
If you are running a daily backup script and you want to add today’s log data to the bottom of the existing log file without destroying yesterday’s data, you must use the -a (append) flag.
./daily_backup.sh | tee -a master_backup.log
Writing to Multiple Files Simultaneously
The tee command is not limited to a single split. You can specify multiple filenames, and it will write identical copies of the output to every single file, while still displaying it on your screen.
dmesg | tee local_log.txt backup_log.txt /var/log/system_snapshot.txt
Using tee with Sudo for Permissions Errors
One of the most frustrating errors in Linux occurs when you try to redirect output into a protected system file. For example, if you try to append a hostname to your hosts file using standard redirection:
sudo echo "192.168.1.50 local-server" >> /etc/hosts
You will receive a “Permission denied” error. This happens because the sudo privilege only applies to the echo command, not to the redirection bracket (>>) attempting to write to the protected file.
The tee command elegantly solves this problem because it is an actual executable program that can be run with sudo privileges.
echo "192.168.1.50 local-server" | sudo tee -a /etc/hosts
This command correctly elevates the permissions of the tee program, allowing it to successfully append the text into the highly restricted system file. Mastering tee is essential for any serious Linux administrator.