Tee (command)

Last updated
tee
Developer(s) AT&T Bell Laboratories, Mike Parker, Richard Stallman, David MacKenzie, Microware, Jim Hall, JP Software, Microsoft
Initial releaseJune 1974;49 years ago (1974-06)
Written in C
Operating system Unix, Unix-like, Plan 9, Inferno, OS-9, FreeDOS, Windows, ReactOS, IBM i
Platform Cross-platform
Type Command
License FreeDOS: GPL-2.0-or-later
ReactOS: GPLv2
Plan 9: MIT License

In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. [1] It is primarily used in conjunction with pipes and filters. The command is named after the T-splitter used in plumbing. [2]

Contents

Overview

Example usage of tee: The output of ls -l is redirected to tee which copies them to the file file.txt and to the pager less. The name tee comes from this scheme - it looks like the capital letter T Tee.svg
Example usage of tee: The output of ls -l is redirected to tee which copies them to the file file.txt and to the pager less . The name tee comes from this scheme - it looks like the capital letter T

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.

Implementations

The command is available for Unix and Unix-like operating systems, Microware OS-9, [3] DOS (e.g. 4DOS, FreeDOS), Microsoft Windows (e.g. 4NT, Windows PowerShell), and ReactOS. [4] The Linux tee command was written by Mike Parker, Richard Stallman, and David MacKenzie. [5] The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities. [6] The FreeDOS version was developed by Jim Hall and is licensed under the GPL. [7] The tee command has also been ported to the IBM i operating system. [8]

Additionally the sponge [9] command offers similar capabilities.

Unix and Unix-like

tee [ -a ] [ -i ] [ File ... ]

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.

4DOS and 4NT

TEE [/A] file...

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.

Windows PowerShell

tee [-FilePath] <String> [-InputObject <PSObject>] tee -Variable <String> [-InputObject <PSObject>]

Arguments:

Note: tee is implemented as a ReadOnly command alias. The internal cmdlet name is Microsoft.PowerShell.Utility\Tee-Object.

Examples

Unix and Unix-like

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

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,

ls--color=always|tee>(sed"s/\x1b[^m]*m//g">ls.txt)

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

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.

Get-Process|Where-Object{$_.Name-like"svc*"}|Tee-ObjectABC.txt|Where-Object{$_.Handles-gt1000}

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.

See also

Related Research Articles

<span class="mw-page-title-main">AWK</span> Programming language

AWK is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems.

In computing, tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from "tape archive", as it was originally developed to write data to sequential I/O devices with no file system of their own. The archive data sets created by tar contain various file system parameters, such as name, timestamps, ownership, file-access permissions, and directory organization. POSIX abandoned tar in favor of pax, yet tar sees continued widespread use.

In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin), standard output (stdout) and standard error (stderr). Originally I/O happened via a physically connected system console, but standard streams abstract this. When a command is executed via an interactive shell, the streams are typically connected to the text terminal on which the shell is running, but can be changed with redirection or a pipeline. More generally, a child process inherits the standard streams of its parent process.

dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files. On Unix, device drivers for hardware and special device files appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.

<span class="mw-page-title-main">Redirection (computing)</span> Form of interprocess communication

In computing, redirection is a form of interprocess communication, and is a function common to most command-line interpreters, including the various Unix shells that can redirect standard streams to user-specified locations.

wc (Unix) Unix command utility

wc is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.

cksum Unix command

cksum is a command in Unix and Unix-like operating systems that generates a checksum value for a file or stream of data. The cksum command reads each file given in its arguments, or standard input if no arguments are provided, and outputs the file's 32-bit cyclic redundancy check (CRC) checksum and byte count. The CRC output by cksum is different from the CRC-32 used in zip, PNG and zlib.

split is a utility on Unix, Plan 9, and Unix-like operating systems most commonly used to split a computer file into two or more smaller files.

paste is a Unix command line utility which is used to join files horizontally by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output.

<span class="mw-page-title-main">Pipeline (Unix)</span> Mechanism for inter-process communication using message passing

In Unix-like computer operating systems, a pipeline is a mechanism for inter-process communication using message passing. A pipeline is a set of processes chained together by their standard streams, so that the output text of each process (stdout) is passed directly as input (stdin) to the next one. The second process is started as the first process is still executing, and they are executed concurrently. The concept of pipelines was championed by Douglas McIlroy at Unix's ancestral home of Bell Labs, during the development of Unix, shaping its toolbox philosophy. It is named by analogy to a physical pipeline. A key feature of these pipelines is their "hiding of internals". This in turn allows for more clarity and simplicity in the system.

In Unix-like and some other operating systems, find is a command-line utility that locates files based on some user-specified criteria and either prints the pathname of each matched object or, if another action is requested, performs that action on each matched object.

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

more (command) Terminal pager available on various operating systems

In computing, more is a command to view the contents of a text file one screen at a time. It is available on Unix and Unix-like systems, DOS, Digital Research FlexOS, IBM/Toshiba 4690 OS, IBM OS/2, Microsoft Windows and ReactOS. Programs of this sort are called pagers. more is a very basic pager, originally allowing only forward navigation through a file, though newer implementations do allow for limited backward movement.

tail is a program available on Unix, Unix-like systems, FreeDOS and MSX-DOS used to display the tail end of a text file or piped data.

yes (Unix) Unix command

yes is a command on Unix and Unix-like operating systems, which outputs an affirmative response, or a user-defined string of text, continuously until killed.

A filter is a computer program or subroutine to process a stream, producing another stream. While a single filter can be used individually, they are frequently strung together to form a pipeline.

alias (command) Command in various command line interpreters

In computing, alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. alias is available in Unix shells, AmigaDOS, 4DOS/4NT, KolibriOS, Windows PowerShell, ReactOS, and the EFI shell. Aliasing functionality in the MS-DOS and Microsoft Windows operating systems is provided by the DOSKey command-line utility.

pexec is a command-line utility for Linux and other Unix-like operating systems which allows the user to execute shell commands in parallel. The specified code can be executed either locally or on remote hosts, in which case ssh is used to build a secure tunnel between them. Similar to shell loops, a variable is changed as the loop starting the tasks iterates, so that many values can get passed to the specified command or script. pexec is a free software utility, and part of the GNU Project. It is available under the terms of GPLv3, and is part of the current Debian stable release.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a computer program by inputting lines of text called command-lines. Command-line interfaces emerged in the mid-1960s, on computer terminals, as a user-friendly alternative to punched cards.

cat (Unix) Unix command utility

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to (con)catenate files. It has been ported to a number of operating systems.

References

  1. "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.
  2. "In Unix, what do some obscurely named commands stand for?" . Retrieved 3 February 2012.
  3. Paul S. Dayan (1992). The OS-9 Guru - 1 : The Facts. Galactic Industrial Limited. ISBN   0-9519228-0-7.
  4. "reactos/reactos". GitHub. 3 January 2022.
  5. "tee: read from standard input and write to standard output and files". www.mankier.com.
  6. "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
  7. "ibiblio.org FreeDOS Package -- tee (Unix-like)". www.ibiblio.org.
  8. IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM . Retrieved 5 September 2020.
  9. "sponge(1): soak up stdin/write to file - Linux man page". linux.die.net.
  10. "GNU Coreutils: tee invocation" . Retrieved 3 February 2016.

Further reading