Streamline Your Linux Output with the Tee Command

📆 · ⏳ 2 min read · ·

Introduction

As a Linux user, you may frequently encounter situations where you need to redirect output to multiple destinations. For example, you may want to save a copy of your output to a file while also sending it to another command for further processing.

This can be a time-consuming and error-prone task if done manually. The tee command is a simple yet powerful tool that can help you achieve this.

The tee command reads input from standard input or a file and writes it to both standard output and one or more files simultaneously. This makes it a great tool for redirecting output to multiple destinations without having to manually copy and paste the data.

In this article, we’ll go through how to use the tee command on Linux.

Using the Linux Tee Command

Using tee for Redirecting Output

The basic syntax of the tee command is as follows:

Terminal window
command | tee [options] [file ...]

In this syntax, command is the command whose output you want to redirect, and file is the name of the file or files you want to send the output to.

If you omit the file name, tee will write the output to standard output.

For example, the following command will list the contents of the current directory and save a copy of the output to a file named “dirlist.txt”:

Terminal window
ls | tee dirlist.txt

You can also use tee to send output to multiple commands simultaneously.

The following command will list the contents of the current directory and send the output to both the grep and sort commands:

Terminal window
ls | tee >(grep "file") >(sort)

In this command, the > symbol before each command specifies that tee should send the output to that command.

Using tee with Options

The tee command also supports several options that can modify its behavior. For example, you can use the -a option to append the output to an existing file instead of overwriting it:

Terminal window
ls | tee -a dirlist.txt

You can also use the -i option to ignore the interrupt signal (SIGINT) and continue running even if you press Ctrl+C:

Terminal window
ls | tee -i dirlist.txt

Conclusion

The Linux tee command is a useful tool that can simplify the process of redirecting output to multiple destinations. Whether you need to save a copy of your output to a file, send it to another command, or both, tee can help you achieve this with ease.

I hope this article has provided you with a solid understanding of how to use the tee command on Linux. Happy output redirecting!

You may also like

  • HTTPS with self-signed certificates for your Homelab services

    In this article we will deep dive into understanding how we can setup HTTPS with self-signed certificates for our Homelab services.This is often required when you are running your own services and you want to access them over HTTPS.

  • Setup Shareable Drive with Samba in Linux

    In this article we will setup a shareable drive in Linux with SMB. We will learn how to setup the share directory using Samba on server and how to mount it on client.

  • Setup Shareable Drive with NFS in Linux

    In this article we will learn how to setup a shareable drive with NFS in Linux. We will see the steps to setup NFS server and mount the drive on a client machine.