How to Use Tee Command on Linux

Tee Command on Linux

Linux, the versatile and powerful operating system, offers a plethora of commands and utilities that can make your life as a sysadmin or developer easier. Among these, the tee command stands out as a simple yet incredibly useful tool for manipulating and redirecting data streams. Whether you’re a seasoned Linux pro or just getting started, mastering the tee command is a valuable skill that can save you time and effort in various tasks.

In this comprehensive guide, we will delve deep into the world of the tee command. We’ll start with the basics and gradually move towards advanced usage, providing you with step-by-step instructions, real-world examples, troubleshooting tips, and additional resources to ensure you become a tee command ninja.

Understanding the Tee Command

What is the Tee Command?

At its core, the tee command allows you to read from standard input and write to standard output and files simultaneously. This seemingly simple functionality opens up a world of possibilities, making it a must-know tool for Linux enthusiasts.

How Tee Works

Before diving into practical examples, let’s understand the inner workings of the tee command. When you use tee, it reads data from standard input and writes it to both standard output (usually the terminal) and one or more specified files. This dual output capability is what makes tee so powerful.

Basic Usage

Basic Syntax

The basic syntax of the tee command is straightforward:

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

Here’s a breakdown of the components:

  • command: The command whose output you want to capture and manipulate.
  • |: The pipe symbol, which redirects the output of command to tee.
  • tee: The tee command itself.
  • [options]: Optional flags that modify tee‘s behavior.
  • [file...]: One or more filenames where you want to save the output.

Output to a File

Let’s start with a practical example. Suppose you have a log file named mylog.txt, and you want to append some data to it. You can use tee like this:

echo "New log entry" | tee -a mylog.txt

Here’s what this command does:

  • echo "New log entry": Generates a new log entry.
  • |: Redirects the output of echo to tee.
  • tee -a mylog.txt: Writes the output to both the terminal and the mylog.txt file, with the -a option ensuring that the data is appended to the file.

Display Output on Terminal

By default, tee displays the data on the terminal. For example:

ls | tee mylist.txt

In this command, the output of ls is both saved to mylist.txt and displayed on the terminal.

Advanced Usage

Piping with Tee

One of the most powerful features of tee is its ability to work with pipes. Consider this scenario: you want to search for a specific keyword in a large log file and simultaneously save the result to another file. You can do this by combining grep with tee:

grep "error" mylog.txt | tee error.log

Here, grep filters the lines containing “error” from mylog.txt, and tee saves these lines to error.log while also displaying them on the terminal.

Tee with Command Substitution

Command substitution allows you to capture the output of a command and use it as an argument for another command. When combined with tee, it can be a game-changer. For instance, you can capture the current date and time and save it to a file:

echo "The current date and time is: $(date)" | tee datetime.txt

In this example, $(date) is replaced with the current date and time, and the entire string is then passed to tee, which saves it in datetime.txt.

Tee with Standard Error (stderr)

In Linux, standard error (stderr) is used for error messages. You can use tee to capture and manage both standard output (stdout) and standard error (stderr). For example:

ls /nonexistentfolder 2>&1 | tee error_output.txt

Here, 2>&1 redirects stderr to stdout, allowing you to capture both normal and error output in error_output.txt.

Tee Command Options

-a, –append

The -a option allows you to append data to an existing file instead of overwriting it. For instance, you have a file called mylog.txt, and you want to add new log entries to it:

echo "New log entry" | tee -a mylog.txt

This ensures that the new entry is appended to mylog.txt without deleting the existing content.

-i, –ignore-interrupts

The -i option makes tee ignore interrupt signals (e.g., pressing Ctrl+C). This can be handy when you want to ensure that data is written to a file even if you decide to interrupt the process.

some_command | tee -i output.txt

With this option, even if you interrupt some_command, the output will still be saved in output.txt.

-p, –output-error

The -p option is used to handle write errors. It prevents tee from exiting if a write error occurs, allowing you to continue processing data.

some_command | tee -p output.txt

This can be useful in situations where you want to capture as much data as possible, even if there are occasional write errors.

Real-world Examples

Logging System Output

Sysadmins often need to monitor and log system activity. The tee command can be a lifesaver in such scenarios. Let’s say you want to monitor system resource usage and save it to a log file:

top -b -n 1 | tee system_stats.log

Here, top provides real-time system statistics, and tee saves the output in system_stats.log, allowing you to review historical data.

Monitoring Network Traffic

Network administrators frequently use tee to monitor network traffic. For instance, you can capture network packets with tcpdump and save them to a file for analysis:

tcpdump -i eth0 -w capture.pcap | tee capture.log

In this example, tcpdump captures network traffic on the eth0 interface and saves it to both capture.pcap and capture.log.

Data Transformation

Tee can also be handy for data transformation tasks. Let’s say you want to process a CSV file and simultaneously create a backup:

cat data.csv | tee backup.csv | sed 's/,/|/g' > transformed_data.csv

Here, cat reads the data.csv file, tee creates a backup in backup.csv, and sed transforms the data by replacing commas with vertical bars, saving the result in transformed_data.csv.

Tips and Best Practices

Tee Command Efficiency

To make the most of the tee command, keep these tips in mind:

  • Use tee when you need to split or duplicate output streams.
  • Combine tee with pipes to create powerful data processing pipelines.
  • Be mindful of resource usage when processing large amounts of data with tee.


While tee is a reliable tool, you may encounter issues. Here are some troubleshooting tips:

  • If you’re not seeing the expected output, check your command syntax and file permissions.
  • Ensure that the files you’re writing to with tee exist and are writable.
  • Use the -i and -p options to handle interruptions and errors gracefully.


Congratulations! You’ve now mastered the tee command in Linux, a versatile tool that can streamline your data manipulation tasks. Whether you’re a sysadmin, developer, or just a Linux enthusiast, tee is a valuable addition to your toolkit.

In this guide, we’ve covered the basics of tee, its advanced usage, options, real-world examples, and best practices. With this knowledge, you can efficiently redirect and manipulate data streams, saving time and simplifying complex tasks.

Keep exploring the world of Linux commands and utilities. Your journey to becoming a Linux pro has just begun. Happy tinkering!

Back to top button