How To Use the Tee Command in Linux

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 uses the follows the basic 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.
-a
or--append
: Appends the output to a file instead of overwriting it.-i
or--ignore-interrupts
: Ignores interrupt signals.-h
or--help
: Displays the command usage and supported options.-v
or--version
: Displays thetee
version information.
Follow the basic usage steps below to test the tee
command on your Linux workstation.
View the
tee
command 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 exit
View the
tee
command 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
-i
option to preventtee
command from terminating when receiving an interrupt signal such as Ctrl + C.console$ tee -i
The
-i
option is useful when running long commands to ensure the command is successful without any user interruption. Press Ctrl + Z to terminate thetee
command 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.txt
file using thetee
command with aHello World
string.console$ echo "Hello World" | tee output.txt
Output:
Hello World
View the
output.txt
file contents and verify that theHello World
string is available.console$ cat output.txt
Output:
Hello World
Create another file
merged.txt
using the contents of theoutput.txt
file.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.txt
file and verify that it includes aHello World
string.console$ cat output.txt
Output:
Hello World
Overwrite the
output.txt
file with a newGreetings from Vultr
string.console$ echo "Greetings from Vultr" | tee output.txt
Your output should be similar to the one below:
Greetings from Vultr
View the
output.txt
file 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.txt
file with a newHello From Vultr Docs
string.console$ echo "Hello From Vultr Docs" | tee -a output.txt
Output:
Hello From Vultr Docs
View the
output.txt
file 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
tee
command by specifying the file names directly.console$ tee <filename> <filename2> <filename3> ....
For example, create multiple files using the
tee
command with aThe Data Backup Process is Complete
string as the common value.console$ echo "The Data Backup Process is Complete" | tee file1.txt file2.txt
View the
file1.txt
andfile2.txt
contents to verify the change.console$ cat file1.txt file2.txt
Output:
The Data Backup Process is Complete The Data Backup Process is Complete
Based on the above output, the
tee
command created multiple files,file1.txt
andfile2.txt
with the sameThe Data Backup Process is Complete
string. You can use thetee
command to create, overwrite, or append data in multiple files.
Write multi-line content to multiple files such as
test1.txt
,test2.txt
using thetee
command 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
tee
command and the<<EOF
delimiter to write multi-line content to thetest1.txt
andtest2.txt
files.View the
test1.txt
andtest2.txt
file 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.local
entry to the/etc/hosts
file using thetee
command 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.local
line to the/etc/hosts
file which requires sudo user privileges. Without using thesudo
command, you will receive apermission denied
error because the/etc/hosts
file is owned by the root user.Output:
192.0.2.1 mycustomhost.local
View the
/etc/hosts
file 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
ls
command to thetee
command 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
tee
command to write the results in alist.txt
file.View the
list.txt
file 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.txt
Use the
tee
command with multiple pipes to filter and redirect output. For example, run the following command to filter thels
command usingtee
andgrep
to redirect output to afinal.txt
file.console$ ls | tee intermediate.txt | grep .txt > final.txt
The above command pipes the output of the
ls
command to thetee
command which saves it asintermediate.txt
. Then, thegrep
command filters theintermediate.txt
output to sort all.txt
files and redirects the output to thefinal.txt
file.View the
intermediate.txt
file contents to verify thetee
command change.console$ cat intermediate.txt
View the
final.txt
file 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
tee
command with advanced piping to filter and sort output. For example, use thetee
command withawk
andsort
to filter the active processes to save the final output to asorted_process_list.txt
file.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
tee
command to aprocess_list.txt
file. Then, theawk
command extracts the first and second columns (user and process ID), while thesort
command uses the default ascending lexicographical order to write the final output to asorted_process_list.txt
using thetee
command.View the
process_list.txt
file and verify that the full process information is available.console$ cat process_list.txt
View the
sorted_process_list.txt
file 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.