The tee command converts a standard output into a standard input and also can write this to files. So tee is practically only about writing to a file. The cat command is for reading (both input and files) and writing (with > and >>) to files. cat is a powerful and safe tool. tee is used in specific advanced complex cases. For the below function, you can not use cat:
1 2 3 4 | // 23.227.167.33 is our one server's IP at the time of writing this guide # ping 23.227.167.33 | tee [-a] ping_report.txt # |
cat is even safer than Echo. The echo command can erase a file:
1 2 3 | # echo " " > /etc/mysql/mariadb.cnf # |
If improperly executed, the tee can do a similar thing. So, tee and echo should be carefully used. The file into which Tee saves the read data can be newly created when necessary. The reason for using the tee is to preserve intermediate results within a Linux pipe.
---
Do not use a tee on a production server for learning purposes. Instead, learn to use tee on a local computer and then use it on a production server.
How is the tee Command Used?
The basic format of the tee command looks like this:
1 | tee [OPTIONS] [FILE] |
Here is an example for the use:
1 | du -h | tee disk_usage.txt |
If you want to add the new content, add the -a
flag and the command would look like this:
1 | du -h | tee -a disk_usage.txt |
![Using the tee Command and Difference With cat Command](https://thecustomizewindows.cachefly.net/wp-content/uploads/2023/04/Using-the-tee-Command-and-Difference-With-cat-Command.png)
To understand the function, run:
1 | date | tee time.txt |
If you run the command for a second time, it will get overwritten. If you add a -a
flag, it will append a new line:
1 | date | tee -a time.txt |
We can echo and write:
1 | echo "Visit https://thecustomizewindows.com" | sudo tee -a test_document.txt |
The tee command simply creates an N+1 number of lines or files.