Linux Terminal Command: tee

The tee command is an essential tool in Text Processing & Piping. In this tutorial, we will explore what tee does, look at everyday examples, and cover advanced options to supercharge your command-line workflow.


Concept & Explanation

The tee command duplicates the output stream, saving it to files while printing it to standard output.


Common Options & Syntax

tee [options] [arguments]

Here are the most common flags used with tee:


1. Interactive Example (Simple)

Here is how most people run the command:

# Example
ls -l | tee file_list.txt

What it does: Saves the list of files to ‘file_list.txt’ and prints it to the screen.


2. Power-User Example (Advanced)

For scripting and advanced diagnostics, use this configuration:

# Advanced
echo '127.0.0.1 db.local' | sudo tee -a /etc/hosts

What it does: Appends (-a) the hostname setting to /etc/hosts using sudo, letting you write to root-owned files from regular user pipes.


⚙️ Warning & Common Pitfalls

[!WARNING] Standard redirection sudo echo '...' >> /etc/hosts fails because redirection is executed by the shell without sudo privileges. Using tee resolves this safely.


Here are some related posts on cli_tty1 you might want to check out: