Tee (Unix)
Encyclopedia
In computing, tee is a command
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] [-InputObject ]
tee -Variable [-InputObject ]

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 character
    Wildcard 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 (lint
    Lint programming tool
    In 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:



lint program.c | tee program.lint


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:



lint program.c | tee -a program.lint


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:



echo "Body of file..." | sudo tee root_owned_file > /dev/null


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 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:


c:\> 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
Ipconfig
ipconfig 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 window
Win32 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 processes
    Process (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 handles
    Handle (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:



Get-Process | where-Object { $_.Name -like "svc*" } | Tee-Object ABC.txt | Where-Object { $_.Handles -gt 1000 }


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

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK