Our Knowledge Base provides step-by-step guides, troubleshooting tips, and expert insights to help you manage VPS, dedicated servers, domains, DDoS protection, and more — all designed to make your experience with us fast, secure, and stress-free.
In this article, we’ll cover the basics of using the tee command.
The tee command reads from the standard input and writes to both standard output and one or more files at the same time.
The syntax for the tee command is as follows:
tee [OPTIONS] [FILE]
OPTIONS :
-a (–append) – Do not overwrite the files instead append to the given files.
-i (–ignore-interrupts) – Ignore interrupt signals.
Use tee –help to view all available options.
FILE_NAMES – One or more files. Each of which the output data is written to.
The most basic usage of the tee command is to display the standard output (stdout) of a program and write it in a file.
df -h | tee disk_usage.txt
Filesystem Size Used Avail Use% Mounted on devtmpfs 2.9G 0 2.9G 0% /dev tmpfs 2.9G 0 2.9G 0% /dev/shm tmpfs 2.9G 297M 2.6G 11% /run tmpfs 2.9G 0 2.9G 0% /sys/fs/cgroup /dev/vda1 23G 1.9G 20G 9% / tmpfs 581M 0 581M 0% /run/user/0
You can view the content of the disk_usage.txt file using the cat command .
The tee command can also write to multiple files. To do so, specify a list of files separated by space as arguments:
command | tee file1.out file2.out file3.out
By default, the tee command will overwrite the specified file.
command | tee -a file.out
To ignore interrupts use the -i (–ignore-interrupts) option. This is useful when stopping the command during execution with CTRL+C and want tee to exit gracefully.
command | tee -i file.out
If you don’t want tee to write to the standard output, you can redirect it to /dev/null:
command | tee file.out >/dev/null
Let’s say you want to write to a file that is owned by root as a sudo user. The following command will fail because the redirection of the output is not performed by sudo. The redirection is executed as the unprivileged user.
sudo echo "newline" > /etc/file.conf
The output will look something like this:
bash: /etc/file.conf: Permission denied
Simply prepend sudo before the tee command as shown below:
echo "newline" | sudo tee -a /etc/file.conf
tee will receive the output of the echo command , elevate to sudo permissions and write to the file.
Using tee in conjunction with sudo allows you to write to files owned by other users.
The tee command reads from standard input and writes it to standard output and one ore more files.