The tee command records from the regular input and writes both standard output and one or more files simultaneously. Tee is frequently used in sequence with other commands through piping.
In this article, we will cover the basics of working the tee command.
tee Command Syntax
The syntax for the tee command is as below:
tee [OPTIONS] [FILE]
Where OPTIONS can be:
- -a (–append) – Do not overwrite the files; instead, affix 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
How to Use the tee Command
The tee command’s most basic method represents the standard output (stdout) of a program and writing it in a file.
- How to Rename Files and Directories in Linux
- Fdisk Command in Linux (Create Disk Partitions)
- How to Rename Directories in Linux
In the below example, we use the df command to get information about the available disk space on the file system. The output is piped to the tee command, expressing the result to the terminal, and writes the same information to the file disk_usage.txt.
$ df -h | tee disk_usage.txt
Filesystem Size Used Avail Use% Mounted on dev 7.8G 0 7.8G 0% /dev run 7.9G 1.8M 7.9G 1% /run /dev/nvme0n1p3 212G 159G 43G 79% / tmpfs 7.9G 357M 7.5G 5% /dev/shm tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup tmpfs 7.9G 15M 7.9G 1% /tmp /dev/nvme0n1p1 511M 107M 405M 21% /boot /dev/sda1 459G 165G 271G 38% /data tmpfs 1.6G 16K 1.6G 1% /run/user/120
Write to Multiple File
By using the tee command, you can write to multiple files also. To do so, define a list of files separated by space as arguments:
$ command | tee file1.out file2.out file3.out
Append to File
By default, the tee command will overwrite the specified file. Use the -a (–append) option to append the output to the 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 the tee to exit gracefully.
$ command | tee -i file.out
Hide the Output
If you don’t want the tee to write to the standard output, you can redirect it to /dev/null:
$ command | tee file.out >/dev/null
Using tee in Conjunction with sudo
Let us say you need to write to a file owned by root as a sudo user. The following command will fail because the redirection of the output is not operated 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
Prepend sudo before the tee command as shown below:
$ echo "newline" | sudo tee -a /etc/file.conf
the tee will receive the echo command output, upgrade to sudo permissions and then write to the file.
Using tee in combination with sudo enables you to write to files owned by other users.
If you want to read from standard input and writes it to standard output and one or more files, then the tee command is used.