tee

utilitieslinux
The tee command is one of the most frequently used commands in Linux/Unix-like operating systems. tee The tee command reads from standard input and writes to both standard output and one or more files simultaneously. This allows you to view output in your terminal while also saving it to a file, making it invaluable for logging and debugging.

Quick Reference

Command Name:

tee

Category:

utilities

Platform:

linux

Basic Usage:

tee [options] [arguments]

Common Use Cases

  • 1

    Text redirection

    Redirect output to both the console and files

  • 2

    Logging

    Capture output for logging and debugging

  • 3

    Scripting

    Use in shell scripts to redirect output programmatically

  • 4

    Data processing

    Manipulate and transform text data

Syntax

tee [OPTION]... [FILE]...

Options

Option Description
-a, --append Append to the given files, do not overwrite
-i, --ignore-interrupts Ignore interrupt signals
-p Diagnose errors writing to non pipes
--output-error[=MODE] Set behavior on write error. See MODE below
--help Display help information
--version Display version information

MODE values for --output-error:

Mode Description
warn Diagnose errors writing to any output
warn-nopipe Diagnose errors writing to any output not a pipe
exit Exit on error writing to any output
exit-nopipe Exit on error writing to any output not a pipe

Examples

How to Use These Examples

The examples below show common ways to use the tee command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

#

Basic Examples:

echo "Hello, World!" | tee file.txt

Write "Hello, World!" to both the terminal and file.txt.

ls -la | tee directory_listing.txt

Display directory listing and save it to a file.

command | tee -a logfile.txt

Append command output to an existing file while also displaying it.

Advanced Examples:

command | tee file1.txt file2.txt file3.txt

Write output to multiple files simultaneously.

command 2>&1 | tee output.log

Capture both standard output and error messages.

make 2>&1 | tee build.log | grep "warning:"

Save full compilation output to a file while only displaying warnings in the terminal.

sudo tee /etc/someconfig.conf > /dev/null < config.txt

Write a file with elevated privileges without using sudo with a text editor.

command | tee >(grep 'error' > errors.log) >(grep 'warning' > warnings.log) > full.log

Using process substitution to filter the output in different ways simultaneously (in Bash).

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Points:

  • The tee command is named after the T-splitter in plumbing, which divides a pipe into two separate paths
  • It's particularly useful for logging the output of commands that take a long time to run
  • When used with sudo, tee can write to files that require elevated privileges
  • Unlike simple redirection (>), tee shows the output in the terminal while also saving it
  • It's commonly used in pipelines to process intermediate results while preserving the original data

Common Use Cases:

  • Debugging scripts by logging output while still seeing it on screen
  • Keeping build logs while monitoring the build process
  • Modifying system files that require root privileges
  • Creating multiple copies of data streams for different processing paths
  • Saving command output that would otherwise only be displayed temporarily
  • Recording system information while viewing it at the same time

Related Commands:

  • cat - Concatenate and display files
  • echo - Display text or variables in the terminal
  • script - Make a typescript of a terminal session
  • logger - Log messages to the system log
  • dd - Convert and copy files with more options for block operations
  • pee (from moreutils) - tee standard input to pipes

Tips for Effective Use:

  • Combine with 2>&1 to capture both standard output and errors
  • Use with -a to build log files across multiple command executions
  • When writing to system files as root, prefer sudo tee over sudo echo > file (which doesn't work)
  • In Bash, tee can be combined with process substitution (>(command)) for complex workflows
  • When using with sudo, send to /dev/null to avoid seeing the output twice if you only want it in the file

Tips & Tricks

1

Use the -a option to append to the given file instead of overwriting

2

Use the -i option to ignore interrupt signals

3

Use the -p option to specify a file descriptor to write to

4

Use the -t option to specify a timeout before writing the buffer

5

Use the -h option to display help

Common Use Cases

Text redirection

Redirect output to both the console and files

Logging

Capture output for logging and debugging

Scripting

Use in shell scripts to redirect output programmatically

Data processing

Manipulate and transform text data

Automation

Automate text redirection in scripts

Related Commands

These commands are frequently used alongside tee or serve similar purposes:

Use Cases

1

Text redirection

Redirect output to both the console and files

2

Logging

Capture output for logging and debugging

3

Scripting

Use in shell scripts to redirect output programmatically

4

Data processing

Manipulate and transform text data

5

Automation

Automate text redirection in scripts

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the tee command works in different scenarios.

$ tee
View All Commands