The tee command is tool that reads from standard input and writes to both standard output and one or more files simultaneously.
I find this handy when I'm outputting a file and the stdout at the same time.
Consider the following command
This outputs the results of my df command to a text file. I could do this with the ">" redirect as well.
However, what if I wanted to output to 2 or 3 files at the same time?
tee will let me do that.
Again I could do this with the ">" redirect. But the ">" redirect has some limitations.
For example, consider the following
We will see the line we echo'd out to the file. Now let's run it again.
Oops, what happened, we lost our first line of text, it was overwritten by the second echo command.
So we can get around that by using a double re-direct.
Notice now it didn't erase the first line, it appended a second line.
That's what the -a flag does when you are using tee.
Another nice option when using tee, is that I can ignore system interrupt errors while watching the output.
Like other commands, depending on what you are doing, you may have to use it with sudo.
Hopefully this will be helpful to someone.
I find this handy when I'm outputting a file and the stdout at the same time.
Consider the following command
Code:
df -h | tee disk_usage.txt
This outputs the results of my df command to a text file. I could do this with the ">" redirect as well.
However, what if I wanted to output to 2 or 3 files at the same time?
Code:
ls -l | tee file1.txt file2.txt
tee will let me do that.
Code:
echo "New line" | tee -a file.txt
Again I could do this with the ">" redirect. But the ">" redirect has some limitations.
For example, consider the following
Code:
echo "dos2unix is my favorite command." > favorite.txt
cat favorite.txt
We will see the line we echo'd out to the file. Now let's run it again.
Code:
echo "tee is my second favorite command." > favorite.txt
cat favorite.txt
Oops, what happened, we lost our first line of text, it was overwritten by the second echo command.
So we can get around that by using a double re-direct.
Code:
echo "but dos2unix is still my favorite command." >> favorite.txt
cat favorite.txt
Notice now it didn't erase the first line, it appended a second line.
That's what the -a flag does when you are using tee.
Another nice option when using tee, is that I can ignore system interrupt errors while watching the output.
Code:
some_command | tee -i output.txt
Like other commands, depending on what you are doing, you may have to use it with sudo.
Code:
echo "New line" | sudo tee -a /etc/some_file.conf
Hopefully this will be helpful to someone.