Home > File & Directory Management > tee

tee: Send standard input to files and standard output simultaneously

The tee command is a utility that reads standard input, outputs it to standard output, and simultaneously writes it to one or more files. It is commonly used to check intermediate results in pipelines or to save data to multiple files at once. The -a option is used to append to files instead of overwriting them.

Overview

The tee command acts like a 'T' junction in a pipe, sending data from standard input to standard output while also recording it into specified files. This is very useful for monitoring intermediate results in a pipeline or saving the same data to multiple locations. By default, it overwrites the file if it exists, but the -a option allows appending to the existing content.

Key Features

  • Simultaneously sends standard input to standard output and files
  • Overwrites or appends to files
  • Useful for checking intermediate results in pipelines

Key Options

The main options for the tee command control how data is written to files.

Writing Modes

Generated command:

Try combining the commands.

Description:

`tee` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various examples of using the tee command.

Overwrite standard output to a file

echo "Hello World" | tee output.txt

Default behavior, overwrites the file if it exists.

Append standard output to a file

echo "Another line" | tee -a output.txt

Use the -a option to append new content to the end of an existing file.

Write to multiple files simultaneously

ls -l | tee file1.txt file2.txt

Specify multiple file paths to write the same content to them concurrently.

Write to a file with sudo privileges

echo "Restricted content" | sudo tee -a /var/log/system.log

Useful for writing to system files that regular users do not have write permissions for.

Check intermediate results in a pipeline

cat /etc/passwd | grep "root" | tee root_users.txt | wc -l

Use tee in the middle of a pipeline to write data to a file while passing it to the next command.

Tips & Precautions

Useful tips and points to note when using the tee command.

Using with sudo

  • `sudo tee` is very useful for writing content to files that regular users do not have write permissions for. While `echo "content" > /path/to/protected_file` will not have `sudo` applied because redirection is handled by the shell, `echo "content" | sudo tee /path/to/protected_file` allows the `tee` command to run with `sudo` privileges to write to the file.

Pipeline Utilization

  • `tee` acts as a data splitter when placed in the middle of a pipeline. This allows data to be recorded in a file while simultaneously being passed to the next command, making it effective for debugging or logging.

Importance of the -a Option

  • If the `-a` option is not used, `tee` will overwrite the file by default. When adding data to important log files, always use the `-a` option to preserve existing content.

Related commands

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


Same category commands