Innovate | Connect | Transform
ContactService statusUSD $

GICT knowledge base

How to Use the Tee Command in Linux

Published

Linux and Commands

The tee command in Linux is one of those simple yet powerful utilities that often goes unnoticed. It acts like a “T-junction” in plumbing — taking input from one end and splitting it in two directions: one toward the screen (standard output) and another toward one or more files.

In short, the tee command reads from standard input (STDIN) and writes to both standard output (STDOUT) and one or more files at the same time. Unlike redirection operators (> or >>), tee allows you to view the output in real-time while still saving it to a file — making it a great tool for debugging, logging, and monitoring long-running processes.

This article will explain in detail how to use the tee command, explore its options, and provide practical examples.

Understanding the Tee Command Syntax

The basic syntax for the tee command looks like this:

tee [OPTIONS] [FILE]...

Here’s what each part means:

  • OPTIONS – Modify how tee behaves.
  • FILE – The destination file (or files) where you want to save the output.

Commonly Used Options

  • -a or --append → Append output to an existing file instead of overwriting it.
  • -i or --ignore-interrupts → Ignore interrupt signals like Ctrl + C.
  • --help → Display help and usage information.
  • --version → Show the version of tee.

To quickly view these options, run:
tee --help

And to check the installed version:
tee --version

You’ll see output similar to:
tee (GNU coreutils) 8.32

Basic Usage of the Tee Command

Let’s start with something simple. Suppose you want to display “Hello Linux” on the screen and save it to a file at the same time.

echo "Hello Linux" | tee sample_output.txt

You’ll see:
Hello Linux

If you check the file:
cat sample_output.txt

You’ll find:
Hello Linux

This means the text was both displayed and saved.

Creating and Overwriting Files

By default, tee overwrites files if they already exist. Let’s test this behavior:

First, create a new file:

echo "Welcome to ServerLogs" | tee logs_output.txt

Now, overwrite it:

echo "System rebooted successfully" | tee logs_output.txt

Check the file again:
cat logs_output.txt

Output:
System rebooted successfully

The old content is replaced with the new one.

Appending to Files with Tee

If you don’t want to overwrite a file, use the -a (append) option.

echo "Backup completed at 02:00 AM" | tee -a logs_output.txt

Check the file:
cat logs_output.txt

You’ll see both entries now:
System rebooted successfully Backup completed at 02:00 AM

This is particularly useful for log files or reports where you want to add new information without losing the previous data.

Writing to Multiple Files Simultaneously

The tee command allows writing the same output to multiple files at once — something you can’t do easily with standard redirection.

For example:

echo "Security Scan Complete" | tee report_1.txt report_2.txt

Check both files:
cat report_1.txt report_2.txt

Output:
Security Scan Complete Security Scan Complete

Both files now contain identical content.

You can also use Heredoc syntax for multi-line input:

tee system_log1.txt system_log2.txt <<EOF
System Report:
- CPU Load: Normal
- Disk Usage: 55%
- Uptime: 12 days
EOF

Each file will have the same multi-line report.

Using Tee with Sudo Privileges

Sometimes you need to write output to system files like /etc/hosts or /etc/resolv.conf, which are protected. Regular redirection won’t work with sudo, but tee handles this elegantly.

For example:

echo "203.0.113.10 customdomain.local" | sudo tee -a /etc/system_hosts

This appends the entry to /etc/system_hosts (a renamed example for /etc/hosts). Without sudo, this command would fail due to insufficient permissions.

Combining Tee with Other Commands

The real power of tee shines when you use it in pipelines. You can capture and view data at multiple points within a command chain.

Example 1: Saving and Viewing Directory Listings

ls | tee directory_list.txt

This displays the list of files on-screen and saves it to directory_list.txt.

Example 2: Filtering and Saving Output

You can combine it with tools like grep:

ls | tee temp_list.txt | grep ".txt" > filtered_files.txt

Here’s what happens:

  • ls lists all files.
  • tee saves that list in temp_list.txt.
  • grep filters only .txt files.

The result is saved in filtered_files.txt.

Advanced Usage: Combining Tee with awk, sort, and ps

You can chain tee with multiple commands to analyze system processes.

ps aux | tee all_processes.txt | awk '{print $1, $2}' | sort | tee sorted_users.txt

Explanation:

  • ps aux lists all running processes.
  • tee all_processes.txt saves a copy of the full list.
  • awk ‘{print $1, $2}’ extracts the username and process ID.
  • sort organizes them alphabetically.
  • tee sorted_users.txt saves the final sorted list.

Now check your files:
cat all_processes.txt
cat sorted_users.txt

You’ll find one with detailed data and one neatly sorted summary.

Logging Command Outputs for Troubleshooting

You can also use tee to log real-time outputs of long-running operations like updates or installations.

Example:

sudo apt update | tee /var/log/update_monitor.log

This command displays the output in your terminal and saves it into /var/log/update_monitor.log for future analysis.

Conclusion

The tee command might look simple, but it’s one of the most versatile and powerful utilities in Linux. Whether you’re logging system activity, debugging pipelines, or managing files with elevated privileges, tee helps you capture output without losing visibility.

  • Create or update files easily.
  • Append logs dynamically.
  • Write to multiple files simultaneously.
  • Combine with other commands for efficient automation.

By mastering the tee command, you’ll make your Linux workflow more efficient, traceable, and flexible — especially when working with pipelines or real-time logs.

Related guides

Account services are unavailable until the production backend is configured.