Mastering Linux Tee Command: Step-by-Step Examples and Insights

Dive deep into the Linux tee command with our in-depth guide. Learn through real-world examples, and get actionable insights to become proficient.
Oliver Brown, Contributing Editor | Updated October 20, 2023

Table of Contents

table of contents

Navigating through the Linux environment demands familiarity with a range of commands that facilitate efficient data handling and operations. Among these, the “tee” command plays a significant role. As you prepare to delve deeper, anticipate enhancing your Linux command line expertise as we explore the nuances of the “tee” command with a step-by-step tutorial enriched with examples.

Here’s a concise preview of the Linux “tee” command to gear up for a fulfilling learning journey. Let’s begin with understanding the essence and functionality of this powerful tool.

Understanding the Linux Tee Command

The Linux “tee” command is a staple for individuals aiming to streamline their work process on the Linux command line. Before diving into the specifics, let’s have a brief understanding of the place the “tee” command holds in the Linux ecosystem.

The Linux “tee” command functions as a middleware in the pipeline of Linux commands, assisting users in reading from the standard input and writing to both standard output and one or many files simultaneously. In this segment, we’ll dissect its syntax, options, and utility, facilitating a comprehensive understanding.

Syntax and Options

In this section, we will cover the general syntax of the “tee” command along with various options that can be used to modify its behavior. The general syntax of the “tee” command is as follows:

command1 | tee [options] file1 file2 … fileN

Here is a brief explanation of the elements:

  • command1: The initial command whose output will be piped to the “tee” command.
  • [options]: Optional flags that modify the behavior of the “tee” command.
  • file1, file2, …, fileN: The files where the output will be saved.


Let’s take a closer look at the options available with the “tee” command:

  • -a, –append – This option allows you to append the output to the files rather than overwriting them.
  • -i, –ignore-interrupts – Using this option will make the “tee” command ignore interrupt signals.

In the subsequent sections, we will delve deeper and explore various examples to demonstrate the versatility of the “tee” command in real-world scenarios.

Making Effective Use of the Tee Command

Harnessing the potential of the “tee” command in Linux goes beyond just understanding its syntax and options. In this segment, we shall demonstrate its usage through a variety of examples, paving the way for you to integrate this powerful command into your daily tasks seamlessly.

Simple Uses and Examples

To ease into the vast capabilities of the “tee” command, we’ll initiate with some straightforward examples to get the hang of its primary function.

Writing Output to a Single File

Consider a situation where you want to view the output of a command and save it to a file simultaneously. Here’s how you can do it using the “tee” command:

ls -l | tee filelist.txt

In this example, the output of the ls -l command will be displayed on the terminal and saved to the file filelist.txt.

Appending Output to a File

As mentioned earlier, the -a option can be used to append output to a file instead of overwriting it. Here’s an example:

echo “Additional Line” | tee -a filelist.txt

Here, the “Additional Line” string will be added to the end of filelist.txt without overwriting its existing content.

The forthcoming sections will unfold more complex examples and tips to fully exploit the “tee” command’s capabilities.

Advanced Tips and Insights for Using the Tee Command

As you gradually get accustomed to the “tee” command, you’ll find that it offers a lot more than meets the eye. In this section, we venture into some advanced tips and insights that can enhance your Linux command line efficiency manifold.

Redirecting Output to Multiple Files

The “tee” command isn’t limited to writing output to a single file; it can handle multiple files at once. Here’s how:

ls -l | tee file1.txt file2.txt

In this instance, the output of the ls -l command is saved to both file1.txt and file2.txt concurrently.

Creating a Backup while Editing

Another notable use of the “tee” command is to create a backup of a file while editing it using a stream editor like sed. Here is an example:

sed ‘s/old/new/’ input.txt | tee backup.txt | sed ‘s/test/example/’

This command replaces ‘old’ with ‘new’ in input.txt, saves a backup with the first change to backup.txt, and then further replaces ‘test’ with ‘example’.

Utilizing Tee Command in Scripts and Processes

Transitioning from simple commands to more complex scripts, the “tee” command can be an essential tool to enhance your scripting prowess. In this section, we explore various ways in which “tee” can be integrated into scripts and processes for more efficient and flexible operations.

Logging Script Output

Scripts often generate a considerable amount of output which is crucial to log for future reference or debugging. Here’s how you can employ the “tee” command to log script output:

./ | tee script_log.txt

This command runs and saves all the output to script_log.txt while also displaying it on the screen.

Capturing Error Logs

Beyond capturing standard output, “tee” can be used to capture error outputs as well. Here’s an example:

./ 2>&1 | tee script_log.txt

In this case, both the standard output and standard error (denoted by 2>&1) are logged to script_log.txt.

Monitoring System Processes

Using the “tee” command in conjunction with commands like top or ps can help in monitoring system processes effectively. For instance:

top -b -n1 | tee top_output.txt

This command saves the current snapshot of the top command to top_output.txt.

FAQ Section

What is the primary function of the ‘tee’ command in Linux?

The ‘tee’ command in Linux is utilized to read from the standard input and write to both the standard output and one or several files simultaneously. It proves to be extremely useful when you want to save the output of a command to a file while keeping an eye on the output in the terminal.

Can the ‘tee’ command write to multiple files at once?

Yes, the ‘tee’ command can write the output to multiple files at the same time. The syntax would be as follows:

command | tee file1.txt file2.txt … fileN.txt

This command structure will save the output of the preceding command to all listed files concurrently while displaying it on the terminal.

How do you use the append option (-a) with the ‘tee’ command?

The append option, denoted by -a, allows you to add the output to the files without overwriting the existing content. Here’s an example demonstrating its usage:

echo “Appended Line” | tee -a existingfile.txt

This command will add the string “Appended Line” to the end of existingfile.txt without erasing the content that was already present in the file.


In this comprehensive guide, we journeyed through the manifold functionalities and capabilities of the Linux ‘tee’ command. From its basic syntax and options to its integration in scripts and process monitoring, we endeavored to offer a rounded view of this versatile command. Through a range of examples and insights, we hope that you are now equipped to integrate the ‘tee’ command into your Linux repertoire skillfully.

Remember, practice makes perfect. So, feel free to experiment with the ‘tee’ command using the examples and tips shared in this guide. Happy Linuxing!

Published: October 9, 2023 | Updated: October 20, 2023
Contributing Editor

Oliver is a Contributing Editor with plenty of experience in proofreading, editing, and writing. He delivers in-depth guides, reviews, and articles about all aspects of the web hosting industry. In his spare time, Oliver enjoys spending time with his family, playing guitar and read science fiction.

Your browser is outdated, we recommend you update it to the latest version
or use another more modern.