The tee command is normally used to split the output of a program so that it can be both displayed and saved in a file. The command can be used to capture intermediate output before the data is altered by another command or program.
The tee command reads standard input, then writes its content to standard output. It simultaneously copies the data into the specified file(s) or variables.
The syntax differs depending on the command's implementation.
Note: If a write to any successfully opened File operand is not successful, writes to other successfully opened File operands and standard output will continue, but the exit value will be >0.
4DOS and 4NT
TEE [/A] file...
Arguments:
file One or more files that will receive the "tee'd" output.
Flags:
/A Append the pipeline content to the output file(s) rather than overwriting them.
Note: When tee is used with a pipe, the output of the previous command is written to a temporary file. When that command finishes, tee reads the temporary file, displays the output, and writes it to the file(s) given as command-line argument.
Windows PowerShell
tee [-FilePath] <String> [-InputObject <PSObject>]
tee -Variable <String> [-InputObject <PSObject>]
Arguments:
-InputObject <PSObject> Specifies the object input to the cmdlet. The parameter accepts variables that contain the objects and commands or expression that return the objects.
-FilePath <String> Specifies the file where the cmdlet stores the object. The parameter accepts wildcard characters that resolve to a single file.
-Variable <String> A reference to the input objects will be assigned to the specified variable.
Note: tee is implemented as a ReadOnlycommand alias. The internal cmdlet name is Microsoft.PowerShell.Utility\Tee-Object.
Examples
Unix and Unix-like
To view and save the output from a command (lint) at the same time:
lintprogram.c|teeprogram.lint
This displays the standard output of the command lint program.c at the computer, and at the same time saves a copy of it in the file program.lint. If a file named program.lint already exists, it is deleted and replaced.
Similarly, both the stdout and stderr output streams can be redirected to standard output and the program.lint using stream redirection:
lintprogram.c2>&1|teeprogram.lint
To view and append the output from a command to an existing file:
lintprogram.c|tee-aprogram.lint
This displays the standard output of the lint program.c command at the computer and at the same time appends a copy of it to the end of the program.lint file. If the program.lint file does not exist, it is created.
To allow escalation of permissions:
cat~/.ssh/id_rsa.pub|sshadmin@server"sudo tee -a /root/.ssh/authorized_keys2 > /dev/null"
This example shows tee being used to bypass an inherent limitation in the sudo command. sudo is unable to pipe the standard output to a file. By dumping its stdout stream into /dev/null, we also suppress the mirrored output in the console. The command above gives the current user root access to a server over ssh, by installing the user's public key to the server's key authorization list.
In Bash, the output can be filtered before being written to the file—without affecting the output displayed—by using process substitution. For example,
removes common ANSI escape codes before writing to ls.txt, but retains them for display.[10]
4DOS and 4NT
This example searches the file wikipedia.txt for any lines containing the string "4DOS", makes a copy of the matching lines in 4DOS.txt, sorts the lines, and writes them to the output file 4DOSsorted.txt:
>find "4DOS" wikipedia.txt | tee 4DOS.txt | sort > 4DOSsorted.txt
Windows PowerShell
To view and save the output from a command at the same time:
ipconfig|tee OutputFile.txt
This displays the standard output of the command ipconfig at the console window, and simultaneously saves a copy of it in the file OutputFile.txt.
To display and save all running processes, filtered so that only programs starting with svc and owning more than 1000 handles are output:
This example shows that the piped input for tee can be filtered and that tee is used to display that output, which is filtered again so that only processes owning more than 1000 handles are displayed, and writes the unfiltered output to the file ABC.txt.
Windows Powershell is not suitable for binary and raw data and will always treat the stream as text and will modify the data as it is transferred.
^"Man Page for tee (posix Section 1)". IEEE Std 1003.1, 2003 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6. Retrieved 1 December 2013.