Home > Text Processing & Search > tee

Complete Guide to the tee Command: Write to Standard Output and Files Simultaneously

The `tee` command acts like a 'T'-shaped pipe, sending data from standard input to standard output (screen) while simultaneously saving it to one or more files. It is extremely useful in complex pipelines for real-time monitoring of intermediate results and logging them to a file.

tee Command Overview

`tee` is primarily used with pipes (|) in UNIX-like systems and greatly helps in visually monitoring and recording data flow by simultaneously outputting command results to the screen and a file. This is particularly useful when logging script execution results or tracking command execution processes.

How tee Works

It copies data coming through a pipe, sending one copy to standard output and saving the other to a specified file. It's like a 'T'-shaped water pipe that splits water into two branches, hence the name `tee`.

Key Options

The tee command is very easy to use, and you can control how files are saved with a few options.

1. File Writing Mode

2. Error Handling

Generated command:

Try combining the commands.

Description:

`tee` Executes the command.

Combine the above options to virtually execute commands with AI.

Commonly Used Examples

Learn how to manage data in various situations using the `tee` command.

Output ls command results to screen and file simultaneously

ls -l | tee file_list.txt

Displays the file list of the current directory on the screen and saves its content to `file_list.txt`.

Append to file content

echo "--- New Content ---" | tee -a file_list.txt

Appends new content to the end of the `file_list.txt` file. Unlike the `>` (redirection) symbol, the content is also outputted to the screen.

Save to multiple files simultaneously

echo "Save to two files simultaneously" | tee file1.txt file2.txt

Saves the command's output to both `file1.txt` and `file2.txt` simultaneously. You can separate them with a comma or a space. A comma is the correct usage.

Write to a file with administrator privileges

echo "some text" | sudo tee -a /etc/some_file.conf

When using pipes, `>` redirection might fail because it does not inherit `sudo` privileges. Using `tee` can resolve this issue and allow you to append content to a file with administrator privileges.

Package Installation

`tee` is typically included by default in most Linux/Unix systems, so no separate installation is usually required. If it's unavailable in a specific environment, you can install the core utilities package.

Debian/Ubuntu

sudo apt install coreutils

Check and install coreutils package

CentOS/RHEL/Fedora

sudo yum install coreutils

Check and install coreutils package


Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands