Skip to content

tee Linux Command Guide

The Linux tee command is used to read from standard input and write to both standard output and files simultaneously. It allows users to see data on the screen while saving a copy of it to a file. This command is particularly useful when you want to monitor the progress of a command that produces a lot of output or when you want to save the output for later analysis. By default, tee overwrites existing files, but you can use the -a option to append to the existing files instead.

Terminal window
tee [option] [file(s)]
OptionDescription
-aAppend to the given files
-iIgnore interrupt signals
ParameterDescription
file(s)File(s) to output to
Terminal window
ls /etc | tee output.txt

Redirects the output of the “ls /etc” command to both the standard output and a file named “output.txt”.

Terminal window
ls /var/log | tee -a logs.txt

Appends the output of the “ls /var/log” command to both the standard output and a file named “logs.txt”.

Terminal window
tee output.txt < input.txt

Reads the contents of “input.txt”, displays them on the standard output, and saves them into “output.txt”.

Terminal window
sudo ls /root | tee output.txt

Uses “sudo” to list the contents of “/root”, displays the output on the standard output, and saves it into “output.txt”.

Ignoring Standard Output and Only Saving to a File

Section titled “Ignoring Standard Output and Only Saving to a File”
Terminal window
ls /home | tee -a output.txt > /dev/null

Lists the contents of “/home”, appends the output to “output.txt”, and ignores the standard output using “/dev/null”.

Terminal window
ps aux | tee processes.txt | grep root > root_processes.txt

Lists all processes using “ps aux”, saves the complete output to “processes.txt”, and saves only those lines containing “root” to “root_processes.txt”.

Terminal window
ls /directorythatdoesnotexist 2>&1 | tee error_output.txt

Attempts to list the contents of a nonexistent directory, combines standard output and error, displays them both, and saves the combined output in “error_output.txt”.

To use the tee command in Linux, execute the following command:

Terminal window
tee --option <value>

The tee command in Linux is used to read from standard input and write to standard output and files at the same time.

How can I append output to an existing file using tee in Linux?

Section titled “How can I append output to an existing file using tee in Linux?”

To append output to an existing file with tee in Linux, use the -a flag. Here is an example:

Terminal window
echo "Additional content" | tee -a file.txt

How can I display output in the terminal and save it to a file simultaneously using tee in Linux?

Section titled “How can I display output in the terminal and save it to a file simultaneously using tee in Linux?”

To display output in the terminal and save it to a file at the same time with tee in Linux, use it without any flags. For example:

Terminal window
ls | tee list_of_files.txt

Is there a way to ignore errors and continue with tee in Linux?

Section titled “Is there a way to ignore errors and continue with tee in Linux?”

To ignore errors and continue with tee in Linux, you can redirect stderr to stdout. Here is an example command:

Terminal window
command 2>&1 | tee output.txt

Yes, tee can be used with sudo in Linux to save output to a file that requires elevated privileges. Here is an example:

Terminal window
echo "Content that requires sudo" | sudo tee file.txt
  • Capture and display output in the terminal.
  • Save output to a file while also displaying it in the terminal.
  • Combine the standard output stream with a file.