tee Command Examples in Linux

The tee command reads the standard input, sends the output to the default output device (the CLI), and also copies the output to each specified file. This command enables you to verify the output of a command immediately as well as store that output in a file for later reference. Like xargs, tee typically accepts input from another command using the pipe operator.

When used with the -a option, tee appends the output to each output file instead of overwriting it.

Syntax

The general syntax of the tee command is:

# command [options] [arguments] | tee [options] {file names}

Let’s say you want to check the contents of a directory and also output those contents to a file to process later. You could issue separate commands to do this, or you can use the tee command like so:

# ls -l | tee listing.txt

tee Command Examples

1. Copy standard input to each file, and also to standard output:

# echo "example" | tee path/to/file

2. Append to the given files, do not overwrite:

# echo "example" | tee -a path/to/file

3. Print standard input to the terminal, and also pipe it into another program for further processing:

# echo "example" | tee /dev/tty | xargs printf "[%s]"

4. Create a directory called “example”, count the number of characters in “example” and write “example” to the terminal:

# echo "example" | tee >(xargs mkdir) >(wc -c)
Related Post