Purpose Description
In executing the Linux command, we can redirect the output to the file, such as LS >a.txt, then we can't see the output, if we want to save the output to the file, but also want to see the output on the screen, we can use the tee command. The tee command reads the standard input and outputs the content to both standard output and (multiple) files (read from standard input and write to standard output and files. Copy standard input to each FILE, and also to standard output. If a FILE is-, copy again to standard output. In Info tee, the tee command redirects standard output to multiple files (' tee ': Redirect output to multiple files. The ' Tee ' command copies standard input to standard output and also to any files given as arguments. This is useful when you are want to send some the data down a pipe, but also to save a copy. Note that when using a pipe line, the standard error output from the previous command is not read by the tee.
Common Parameters
Format: Tee
Output to standard output only, because no files are specified.
Format: Tee file
The output to the standard output is saved to file files. If the file does not exist, it is created, and if it already exists, it is overwritten. (If a file being written to does not already exist, it is created. If a file being written to already exists, the data it previously
Contained is overwritten unless the '-a ' option is used.)
Format: Tee-a file
The output to the standard output is appended to the file files. If the file does not exist, it is created, and if it already exists, the content is appended to the end, not overwritten.
Format: Tee-
Output to standard output two times. (A FILE of '-' causes ' tee ' to send another copy of input to standard output, but this is typically not so useful as the Copies are interleaved.)
Format: Tee file1 file2-
Output to standard output two times, while saving to File1 and file2.
using the sample Example One tee command vs. redirect
[Root@web ~]# seq 5 >1.txt
[Root@web ~]# Cat 1.txt
1
2
3
4
5
[Root@web ~]# cat 1.txt >2.txt
[Root@web ~]# Cat 1.txt | Tee 3.txt
1
2
3
4
5
[Root@web ~]# Cat 2.txt
1
2
3
4
5
[Root@web ~]# Cat 3.txt
1
2
3
4
5
[Root@web ~]# cat 1.txt >>2.txt
[Root@web ~]# Cat 1.txt | Tee-a 3.txt
1
2
3
4
5
[Root@web ~]# Cat 2.txt
1
2
3
4
5
1
2
3
4
5
[Root@web ~]# Cat 3.txt
1
2
3
4
5
1
2
3
4
5
[Root@web ~]#
example two repeated output strings using the tee command
[Root@web ~]# echo 12345 | Tee
12345
[Root@web ~]# echo 12345 | Tee-
12345
12345
[Root@web ~]# echo 12345 | Tee--
12345
12345
12345
[Root@web ~]# echo 12345 | Tee--
12345
12345
12345
12345
[Root@web ~]# echo 12345 | Tee----
12345
12345
12345
12345
12345
[Root@web ~]#
[Root@web ~]# echo-n 12345 | Tee
12345[root@web ~]# Echo-n 12345 | Tee-
1234512345[root@web ~]# Echo-n 12345 | Tee--
123451234512345[root@web ~]# Echo-n 12345 | Tee--
12345123451234512345[root@web ~]# Echo-n 12345 | Tee----
1234512345123451234512345[root@web ~]#
example three uses the tee command to save standard error output to a file
[Root@web ~]# ls "*"
LS: *: no file or directory
[Root@web ~]# ls "*" | Tee-
LS: *: no file or directory
[Root@web ~]# ls "*" | Tee Ls.txt
LS: *: no file or directory
[Root@web ~]# Cat Ls.txt
[Root@web ~]# ls "*" 2>&1 | Tee Ls.txt
LS: *: no file or directory
[Root@web ~]# Cat Ls.txt
LS: *: no file or directory
example four lists text content and copies 3 copies
Lists the contents of the text file slayers.story, copying 3 copies, with the file names Ss-copy1, Ss-copy2, Ss-copy3:
[Root@web ~]# cat slayers.story |tee ss-copy1 ss-copy2