Host Your Own AI Agent with OpenClaw - Free 1-Click Setup!

Linux tee Command: Syntax, Options, Examples

The linux tee command solves a specific problem: you want to see command output in your terminal AND save it to a file at the same time. Without tee, you’re choosing one or the other.

What Is the Linux tee Command?

tee reads from standard input and writes to both standard output and one or more files simultaneously. The name comes from T-shaped pipe fittings in plumbing, which split flow in two directions. Same idea here.

How tee Fits Into the Unix Pipeline

In a standard Unix pipeline, data flows left to right through pipes. tee inserts itself into that flow and forks the output: one copy continues down the pipeline, one copy goes to a file. Without it, you’d have to run the command twice or sacrifice terminal visibility.

tee Command Syntax

command | tee [OPTIONS] [FILE...]

The pipe character feeds the preceding command’s output into tee. tee then writes that output to the terminal and to each specified file.

How to Use the tee Command

Write Output to a File and Terminal

Basic usage: run a command and write its output to a file while still seeing it on screen:

ls -la | tee directory_listing.txt

The terminal shows the ls output. directory_listing.txt gets the same content. Without tee, ls > directory_listing.txt would redirect to the file but show nothing on screen.

Append to a File with tee -a

By default, tee overwrites the file each time. The -a flag appends instead:

echo "New entry" | tee -a logfile.txt

This is the pattern for building up log files incrementally in scripts.

Write to Multiple Files at Once

List multiple filenames and tee writes to all of them simultaneously:

uptime | tee file1.txt file2.txt file3.txt

All three files receive identical content. Useful for copying configuration snapshots to multiple locations.

Using sudo with tee for Root-Owned Files

This is where tee earns its keep on production systems. You can’t use output redirection with sudo because the shell handles redirection before sudo runs:

echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.conf

The pipe feeds through tee, which runs as root via sudo and writes to the protected file. To append instead of overwrite, add -a:

echo "new config line" | sudo tee -a /etc/nginx/nginx.conf

Advanced tee Usage Patterns

Suppressing Terminal Output with tee

If you want to save output to a file but not see it on screen, redirect tee’s stdout to /dev/null:

command | tee output.txt > /dev/null

The file still gets written. The terminal sees nothing.

Combining tee with Other Commands

tee can sit in the middle of a pipeline, letting you inspect data at intermediate stages:

cat large_file.txt | tee snapshot.txt | grep "ERROR" | wc -l

snapshot.txt captures the full file. The terminal shows only the count of ERROR lines. Two results from one pass.

tee vs Output Redirection: When to Use Each

Output redirection (>) is simpler when you only need to save to a file and don’t care about terminal output. tee is the right choice when you need both, when you need to write to multiple files simultaneously, or when you need sudo to write to protected paths. If your script runs unattended, redirection is usually sufficient. If someone is monitoring it live, tee keeps the output visible.

FAQ: Linux tee Command

What does the tee command do in Linux?

It reads from standard input and writes to both the terminal and one or more files at the same time. Think of it as a pipe splitter.

How do I append output using tee?

Add the -a flag: command | tee -a filename.txt. Without -a, tee overwrites the file each run.

How do I use sudo with tee?

Pipe to sudo tee: echo “content” | sudo tee /path/to/protected/file. This works because tee, not the shell redirection, does the writing, and tee runs with elevated privileges.

What is the difference between tee and output redirection?

Output redirection (> or >>) only writes to a file. You lose terminal visibility. tee writes to both. Use redirection for background scripts where terminal output doesn’t matter. Use tee when you need to see output and save it.

Scroll to Top