How to use the command 'tee' (with examples)
The tee
command is a command-line utility that reads from standard input and writes to both standard output and files or commands. It is used to split the output of a command, allowing it to be displayed on the screen and redirected into one or more files simultaneously. This article will illustrate several use cases of the tee
command.
Use case 1: Copying stdin to a file and stdout
Code:
echo "example" | tee path/to/file
Motivation:
The motivation for using this example is to save the output of a command to a file while still being able to see it on the terminal. By using tee
, we can achieve this without having to run the same command twice.
Explanation:
echo "example"
: This command prints the string “example” to the standard output.tee path/to/file
: Thetee
command reads the standard input from the pipe (|
) and writes it to both the specified file and the standard output.
Example output:
example
Use case 2: Appending to a file without overwriting
Code:
echo "example" | tee -a path/to/file
Motivation: The motivation for using this example is to append the output of a command to a file without overwriting its contents. This is useful when you want to store the output of multiple commands in the same file.
Explanation:
echo "example"
: This command prints the string “example” to the standard output.tee -a path/to/file
: The-a
option specifies that the output should be appended to the given file rather than overwriting it.
Example output:
example
Use case 3: Printing stdin to the terminal and piping it into another program
Code:
echo "example" | tee /dev/tty | xargs printf "[%s]"
Motivation:
The motivation for using this example is to both display the output of a command on the terminal and process it further using another program. By using tee
, we can redirect the output to /dev/tty
to ensure it is displayed on the terminal before piping it into another program.
Explanation:
echo "example"
: This command prints the string “example” to the standard output.tee /dev/tty
: The/dev/tty
file refers to the current terminal, so the output oftee
will be displayed on the terminal before being further processed.xargs printf "[%s]"
: This command reads the output of the previous command from the pipe (|
) and formats it using theprintf
command.
Example output:
example
[example]
Use case 4: Creating a directory, counting characters, and writing to the terminal
Code:
echo "example" | tee >(xargs mkdir) >(wc -c)
Motivation:
The motivation for using this example is to perform multiple actions on the output of a command. By using tee
with process substitution, we can create a directory using the output and count the number of characters in it simultaneously.
Explanation:
echo "example"
: This command prints the string “example” to the standard output.tee >(xargs mkdir) >(wc -c)
: The>(command)
syntax is used for process substitution. In this case, we are using it twice to create two separate processes that will receive the output oftee
.xargs mkdir
: This command reads the output oftee
from the pipe (|
) and creates a directory with the name specified in the output.wc -c
: This command reads the output oftee
from the pipe (|
) and counts the number of characters in it.
Example output:
example
7
Conclusion:
The tee
command is a versatile tool that allows you to split the output of a command, redirecting it to both files and standard output simultaneously. By understanding the different options and use cases of tee
, you can effectively manage and process command output in various scenarios.