Linux and Unix tee command tutorial with examples

Datetime:2016-08-22 22:49:10         Topic: Unix          Share        Original >>
Here to See The Original Article!!!

Table of contents

What does tee do?

tee copies standard input to standard output. It supports writing whatever it is given from standard input to standard output and also writing to one or more files. The command is named after T splitter used in plumbing.

How to display output and save to a file

To display output and save to a file use tee after a pipe and specify the file or files to write to.

echo 'foo' | tee foo.txt
cat foo.txt

This writes the output of the first command to standard output and to a file called foo.txt

How to save to a file and append output

To use tee and append to a file rather use the -a option.

cat foo.txt
echo 'bar' | tee -a foo.txt
cat foo.txt

How to save the state of a pipe

As data flows through UNIX pipelines it can be useful to take a snapshot of the state of the data. This can be for debugging purposes or to take a backup.

ls ~/ | tee pipe1.txt | grep ^b | tee pipe2.txt | sort -r

How to watch log files and write to a file

In this example there is a requirement to analyse the HTTP2 traffic going through an nginx server. Using tee the live logs can be streamed to standard output and also write to a file for further analysis later.

tail -f /var/log/nginx/access.log | grep --line-buffered "HTTP/2.0" | tee -a http2.log

Further reading



Put your ads here, just $200 per month.