Use tee to read from stdin and output to stdout and files

Share this post to your friends !

This entry is part 10 of 10 in the series Linux commands: Part 6 - Redirection

WriteBash - How to use tee to read from stdin and output to stdout and files? This article, I will introduce you to the tee command.

If you forget what stdin and stdout are, read the article overview of I/O redirection.

About tee command

Linux provides a tee command, but according to the man page, it will read from standard input and write to standard output and files.

It is often used in the pipeline, what does this mean? In the past, most of you only saw pipeline examples of exporting the final data to the screen or file.

But what about in the middle stage? If you want to save data at the middle stage of a pipeline to a file, how will you do it?

Tee commands will help you do that. This command syntax:

$ tee [option]... [file]...

In the pipeline:

$ command-1 | tee [option] [file] | command-n

Example of using tee command

We will make an example quite familiar with the pipeline. List content in the /bin directory.

In the middle stage, we use the tee command to save the data to the file. Next, the tee command will output the data to the grep command in the pipeline for further processing.

# ls /bin | tee ls-bin.txt | grep zip
bunzip2
bzip2
bzip2recover
gunzip
gzip
use-tee-to-read-from-stdin-and-output-to-stdout-and-files Use tee to read from stdin and output to stdout and files
Use tee command to read from stdin and output to stdout and files.

You can see in the image above, previously the folder is empty. When executing the pipeline, we see two things that happen simultaneously.

  • Data from ls command has been written to ls-bin.txt file.
  • The tee command continues to output data for the grep command in the pipeline.

Conclusion

In the process of running long pipelines, you need to log what is happening at the middle stage. Tee command will help you solve that problem.

Continue reading the series«« Previous part: Use tail command to print last part of files
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.