tee is a command in Linux that reads from standard input and writes the output to both standard output and one or more files, effectively duplicating the input. It is typically used in shell scripts to tee command output to both a file and the console.
The Tee command in Linux is mostly used in combination with other commands, it reads the input and in response to that writes the output to one or more files. It does this so that the output can be displayed as well as saved to a file at the same time. In this article, we will learn more about the tee command, from its syntax to its use along with examples to help you understand it better.
The syntax of the tee command is provided below.
tee [OPTIONS]... [FILE]...
Here in this OPTIONS could be the following:
- -a (–append): used to append to add to file and not overwrite it.
- -i : in order to ignore the interrupt signals.
For [FILE] include the filename which can be one or more than one file.
Tee Command in Linux
The basic way in which the tee command is commonly used is to write to a file and display the output i.e stdout (standard output).
Users can write to a single as well as multiple files, hide output, they can append to a file without overwriting it, and also useful to ignore any interrupts while executing the file. Let’s understand these ones by one with examples.
- Write to a File
We can use the tee command to write to a file and to demonstrate this we will use the command “df” to check the file system’s disk space and write all the information that we get as output in a file named “exFile.txt”.
df -h | tee exFile.txt
Now go and check the file “exFile.txt” using the cat command.
- Write To Multiple Files
In order to write to multiple files, we’ll use the same above example but save the content into two different files. All you’ve to do is separate the file names by space.
df -h | tee firstFile.out secondFile.out
Now again if you check both the files using the cat command the content will be displayed.
In this way, we can write to one or multiple files and save our output for later use.
- Append to a File
This option “-a” is extremely useful as it helps in appending to a file without overwriting it. In this way, we can save the previous information and later add new information to the file. For this purpose first, we will create a file “helloFile.txt” and inside write “Hello”.
echo "Hello" | tee -a helloFile.txt
Now we’ll spend the word “World” in it.
echo "World" | tee -a helloFile.txt
Now, if we view the file using the cat command we will see that it appended the last word instead of overwriting it over the previous.
In this way, we can append it into a file without overwriting it.
- Hide the Output
In case you don’t want the output to be displayed on the terminal and simply save it in the file using the command “>/dev/null” along with the tee command.
sudo echo "There?" | tee -a helloFile.txt >/dev/null
Later we checked the output using the cat command.
- Ignore any Interrupts
Sometimes we want to execute a text file or any other format of the file but some interrupts occur and the process stops. To ignore these interrupts and want the tee to exit smoothly we use “-i” along with the tee command.
In the example, we will ping google and use the interrupt command and while it’s executing we will interrupt it with CTRL+C.
ping google.com | tee -i newFile.txt
Here you can see that while the command was running it was interrupted but still it executed smoothly. We can verify the content was added to the file by viewing the file using the cat command:
- Using tee with Sudo
In case you want to write to a file that belongs to root or a sudo user you have to use sudo along with the command.
sudo echo "Please?" | sudo tee -a helloFile.txt
This way you can have access to the file easily.
Sometimes the user wants to write the output they get in the terminal to a file and for this purpose, the tee command is used. In this article we told you various ways how the tee command is used in Linux, we can write to multiple files as well as hide output or append to a file without overwriting it. Examples are provided in the case to help you understand the usage better of each command.