ArrowLeft Icon

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!

EnvelopeOpen IconStay up to date

Get notified when I publish something new, and unsubscribe at any time.

Need help with your software project? Let’s talk

You may also like

  • # linux

    SystemD Timers vs. Cron Jobs

    Explore the world of task scheduling in Linux as we compare the classic Cron Jobs with the modern SystemD Timers. Learn when to use each method and how to set them up to automate your Linux system tasks effectively.

  • # linux

    Mastering Intermediate Linux Commands for Efficient Server Management

    As a Linux server administrator, you may have already learned the basics of Linux commands. However, to manage your server more efficiently, you need to dive deeper into the lesser-known, but equally important intermediate-level commands. In this article, we will cover some of the intermediate-level Linux commands that will help you become a more proficient Linux sysadmin.

  • # linux

    Essential Linux Commands for Server Maintenance at Home

    As a Linux system administrator, it's essential to have a good grasp of the command-line interface. In this blog, we'll explore some of the most common and useful commands used for maintaining a Linux server at home. From handling files to networking, these commands will help you streamline your work and keep your server running smoothly.