Tee (Unix)
Encyclopedia
In computing, tee is a command
in various command-line interpreters (shells
) such as Unix shell
s, 4DOS
/4NT
and Windows PowerShell
, which displays or pipes
the output of a command and copies it into a file or a variable. It is primarily used in conjunction with pipes
and filters
.
The tee command reads standard input, then writes its content to standard output and simultaneously copies it into the specified file(s) or variables.
The syntax differs depending on the command's implementation:
Arguments:
Flags:
The command returns the following exit values (exit status
):
Using process substitution
lets more than one process read the standard output of the originating process.
Read this example from GNU Coreutils, tee invocation.
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.
Arguments:
Flags:
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.
[-InputObject ]
tee -Variable [-InputObject ]
Arguments:
Note: tee is implemented as a
This displays the standard output of the command
This displays the standard output of the
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
This displays the standard output of the command
, and simultaneously saves a copy of it in the file
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
Command (computing)
In computing, a command is a directive to a computer program acting as an interpreter of some kind, in order to perform a specific task. Most commonly a command is a directive to some kind of command line interface, such as a shell....
in various command-line interpreters (shells
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...
) such as Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
s, 4DOS
4DOS
4DOS is a command line interpreter by JP Software, designed to replace the default command interpreter COMMAND.COM in DOS and Windows 95/98/Me. The 4DOS family of programs are meant to replace the default command processor. 4OS2 and 4NT replace CMD.EXE in OS/2 and Windows NT respectively...
/4NT
4NT
Take Command Console , formerly known as 4DOS for Windows NT and 4NT, is a command line interpreter by JP Software, designed as a substitute for the default command interpreter in Microsoft Windows...
and Windows PowerShell
Windows PowerShell
Windows PowerShell is Microsoft's task automation framework, consisting of a command-line shell and associated scripting language built on top of, and integrated with the .NET Framework...
, which displays or pipes
Pipeline (software)
In software engineering, a pipeline consists of a chain of processing elements , arranged so that the output of each element is the input of the next. Usually some amount of buffering is provided between consecutive elements...
the output of a command and copies it into a file or a variable. It is primarily used in conjunction with pipes
Pipeline (Unix)
In Unix-like computer operating systems , a pipeline is the original software pipeline: a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one. Each connection is implemented by an anonymous pipe...
and filters
Filter (Unix)
In Unix and Unix-like operating systems, a filter is a program that gets most of its data from its standard input and writes its main results to its standard output . Unix filters are often used as elements of pipelines...
.
Description and syntax
tee is normally used to split the output of a program so that it can be seen on the display and also be saved in a file. The command can also 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 and simultaneously copies it into the specified file(s) or variables.
The syntax differs depending on the command's implementation:
Unix-like
tee [ -a ] [ -i ] [ File ... ]Arguments:
File
One or more files that will receive the "tee-d" output.
Flags:
-a
Appends the output to the end of File instead of writing over it.-i
Ignores interrupts.
The command returns the following exit values (exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...
):
- 0 The standard input was successfully copied to all output files.
- >0 An error occurred.
Using process substitution
Process substitution
In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell...
lets more than one process read the standard output of the originating process.
Read this example from GNU Coreutils, tee invocation.
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
Temporary file
Temporary files may be created by computer programs for a variety of purposes; principally when a program cannot allocate enough memory for its tasks, when the program is working on data bigger than the architecture's address space, or as a primitive form of inter-process communication.- Auxiliary...
. 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]tee -Variable
Arguments:
-InputObject
Specifies the object input to the cmdlet. The parameter accepts variables that contain the objects and commands or expression that return the objects.-FilePath
Specifies the file where the cmdlet stores the object. The parameter accepts wildcard characterWildcard character-Telecommunication:In telecommunications, a wildcard character is a character that may be substituted for any of a defined subset of all possible characters....
s that resolve to a single file.-Variable
A reference to the input objects will be assigned to the specified variable.
Note: tee is implemented as a
ReadOnly
command alias. The internal cmdlet name is Microsoft.PowerShell.Utility\Tee-Object
.Unix-like
- To view and save the output from a command (lintLint programming toolIn computer programming, lint was the name originally given to a particular program that flagged some suspicious and non-portable constructs in C language source code. The term is now applied generically to tools that flag suspicious usage in software written in any computer language...
) at the same time:
This displays the standard output of the command
lint program.c
at the workstation, 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.- To view and append the output from a command to an existing file:
This displays the standard output of the
lint program.c
command at the workstation 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:
This example shows tee being used to bypass an inherent limitation in the sudo
Sudo
sudo is a program for Unix-like computer operating systems that allows users to run programs with the security privileges of another user...
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.4DOS and 4NT
This example searches the filewikipedia.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
:Windows PowerShell
- To view and save the output from a command at the same time:
This displays the standard output of the command
ipconfigIpconfigipconfig in Microsoft Windows is a console application that displays all current TCP/IP network configuration values and can modify Dynamic Host Configuration Protocol DHCP and Domain Name System DNS settings....
at the console windowWin32 console
Win32 console is a text user interface implementation within the system of Windows API, which runs console applications. A Win32 console has a screen buffer and an input buffer, and is available both as a window or in text mode screen, with switching back and forth available via Alt-Enter...
, and simultaneously saves a copy of it in the file
OutputFile.txt
.- To display and save all running processesProcess (computing)In computing, a process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system , a process may be made up of multiple threads of execution that execute instructions concurrently.A computer program is a...
, filtered so that only programs starting with svc and owning more than 1000 handlesHandle (computing)In computer programming, a handle is a particular kind of smart pointer. Handles are used when application software references blocks of memory or objects managed by another system, such as a database or an operating system....
are outputted:
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
.External links
- An introduction on Linux I/O Redirection "Linux I/O Redirection" with tee