
tee is a built-in Linux command that reads from standard input (STDIN) and simultaneously writes it to standard output (STDOUT) and one or more files. It can be seamlessly integrated with other commands through piping, allowing for flexible and customized workflows. The tee command allows you to view the output in real time, unlike redirections, making it ideal for logging or debugging within pipelines.
This article explains how to use the tee command to write and process data using multiple options.
tee Command Syntax
The tee command follows the syntax below:
$ tee [OPTIONS]... [FILE]...
Within the above command, OPTIONS modify the command behavior while FILE specifies the target file to write the changes to. Below are the supported tee command options.
-aor--append: Appends the output to a file instead of overwriting it.-ior--ignore-interrupts: Ignores interrupt signals.-hor--help: Displays the command usage and supported options.-vor--version: Displays theteeversion information.
Follow the basic usage steps below to test the tee command on your Linux workstation.
View the
teecommand basic usage information.console$ tee --help
Output:
Usage: tee [OPTION]... [FILE]... Copy standard input to each FILE, and also to standard output. -a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals -p diagnose errors writing to non pipes --output-error[=MODE] set behavior on write error. See MODE below --help display this help and exit --version output version information and exitView the
teecommand version.console$ tee --version
Your output should be similar to the one below.
tee (GNU coreutils) 8.30 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.Use the
-ioption to preventteecommand from terminating when receiving an interrupt signal such as Ctrl + C.console$ tee -i
The
-ioption is useful when running long commands to ensure the command is successful without any user interruption. Press Ctrl + Z to terminate theteecommand when using the-i(ignore-interrupts) option.
Create Files Using the tee Command
You can use the tee command to create or update files. The -a (append) option updates a file if it exists while running the command without any option overwrites the file if it exists. Follow the steps below to create files using the tee command on your Linux workstation.
Create a new
output.txtfile using theteecommand with aHello Worldstring.console$ echo "Hello World" | tee output.txt
Output:
Hello WorldView the
output.txtfile contents and verify that theHello Worldstring is available.console$ cat output.txt
Output:
Hello WorldCreate another file
merged.txtusing the contents of theoutput.txtfile.console$ cat output.txt | tee merged.txt
Output:
Hello World
Overwrite Files Using the tee Command
You can use the tee command to overwrite existing files and replace the existing contents. The tee command overwrites existing files by default unless the append option is specified. Follow the steps below to use the tee command to overwrite files.
View the
output.txtfile and verify that it includes aHello Worldstring.console$ cat output.txt
Output:
Hello WorldOverwrite the
output.txtfile with a newGreetings from Vultrstring.console$ echo "Greetings from Vultr" | tee output.txt
Your output should be similar to the one below:
Greetings from VultrView the
output.txtfile and verify the change.console$ cat output.txt
Output:
Greetings from Vultr
Append Files Using the tee Command
You can use the tee command to append existing files without overwriting the existing content. Use the -a (append) option to append a file with new contents as described in the following steps.
Append the
output.txtfile with a newHello From Vultr Docsstring.console$ echo "Hello From Vultr Docs" | tee -a output.txt
Output:
Hello From Vultr DocsView the
output.txtfile contents to verify the change.console$ cat output.txt
Output:
Greetings from Vultr Hello From Vultr Docs
Write to Multiple Files Using the tee Command
You can use the tee command to write similar content to multiple files simultaneously. This is useful when creating or updating multiple files in different locations while displaying the output in your terminal session.
Write to multiple files using the
teecommand by specifying the file names directly.console$ tee <filename> <filename2> <filename3> ....
For example, create multiple files using the
teecommand with aThe Data Backup Process is Completestring as the common value.console$ echo "The Data Backup Process is Complete" | tee file1.txt file2.txt
View the
file1.txtandfile2.txtcontents to verify the change.console$ cat file1.txt file2.txt
Output:
The Data Backup Process is Complete The Data Backup Process is CompleteBased on the above output, the
teecommand created multiple files,file1.txtandfile2.txtwith the sameThe Data Backup Process is Completestring. You can use theteecommand to create, overwrite, or append data in multiple files.
Write multi-line content to multiple files such as
test1.txt,test2.txtusing theteecommand and Heredoc.console$ tee test1.txt test2.txt <<EOF Backup Report - File 1 and File 2 2024 Backup Summary EOF
The above here document uses the
teecommand and the<<EOFdelimiter to write multi-line content to thetest1.txtandtest2.txtfiles.View the
test1.txtandtest2.txtfile content to verify the change.console$ cat test1.txt test2.txt
Output:
Backup Report - File 1 and File 2 2024 Backup Summary Backup Report - File 1 and File 2 2024 Backup Summary
Use the tee Command with Sudo Privileges
Follow the steps below to use the tee command with sudo to perform write specific files that require administrative or elevated user privileges.
Add a new
192.0.2.1 mycustomhost.localentry to the/etc/hostsfile using theteecommand with sudo privileges.console$ echo "192.0.2.1 mycustomhost.local" | sudo tee -a /etc/hosts
The above command appends the
192.0.2.1 mycustomhost.localline to the/etc/hostsfile which requires sudo user privileges. Without using thesudocommand, you will receive apermission deniederror because the/etc/hostsfile is owned by the root user.Output:
192.0.2.1 mycustomhost.localView the
/etc/hostsfile contents to verify the change.console$ cat /etc/hosts
Your output should be similar to the one below.
127.0.0.1 localhost .............. ::1 localhost ip6-localhost ip6-loopback ............... 192.0.2.1 mycustomhost.local
Combine the tee Command with Multiple Commands to Filter and Redirect Output
You can use the tee command to redirect and capture command output while displaying it on the terminal at the same time. Combining the tee command with other commands is important in scenarios that involve complex pipelines where you need to save or filter the output of a specific command. Follow the steps below to combine the tee command with other commands to filter and redirect output to one or more files.
Pipe the output of the
lscommand to theteecommand to write the results to a file, such aslist.txt.console$ ls | tee list.txt
The above command lists files in your working directory and uses the
teecommand to write the results in alist.txtfile.View the
list.txtfile contents to verify the change.console$ cat list.txt
Your output should be similar to the one below:
file1.txt file2.txt list.txt output.txt test1.txt test2.txtUse the
teecommand with multiple pipes to filter and redirect output. For example, run the following command to filter thelscommand usingteeandgrepto redirect output to afinal.txtfile.console$ ls | tee intermediate.txt | grep .txt > final.txt
The above command pipes the output of the
lscommand to theteecommand which saves it asintermediate.txt. Then, thegrepcommand filters theintermediate.txtoutput to sort all.txtfiles and redirects the output to thefinal.txtfile.View the
intermediate.txtfile contents to verify theteecommand change.console$ cat intermediate.txt
View the
final.txtfile contents to verify the filtered and redirected output.console$ cat final.txt
Your output should be similar to the one below.
final.txt intermediate.txt list.txt merged.txt output.txt
Use the
teecommand with advanced piping to filter and sort output. For example, use theteecommand withawkandsortto filter the active processes to save the final output to asorted_process_list.txtfile.console$ ps aux | tee process_list.txt | awk '{print $1, $2}' | sort | tee sorted_process_list.txt
The above command uses advanced piping to write the active process information using the
teecommand to aprocess_list.txtfile. Then, theawkcommand extracts the first and second columns (user and process ID), while thesortcommand uses the default ascending lexicographical order to write the final output to asorted_process_list.txtusing theteecommand.View the
process_list.txtfile and verify that the full process information is available.console$ cat process_list.txt
View the
sorted_process_list.txtfile and verify that a sorted process information list is available.console$ cat sorted_process_list.txt
Conclusion
You have used the tee command in Linux to create and update files. You can use the tee command to perform complex file modification tasks by combining it with multiple tasks. For more information and command options, run the man tee command to view the command manual page.