Linux tee
Command
The tee
command in Linux is a versatile utility that reads from standard input and writes to both standard output and one or more files simultaneously. It is particularly useful when you want to capture and display the output of a command while also saving it to a file.
Basic Syntax:
command | tee file1 file2 ...
Example:
Suppose you want to list all files in a directory, display the list on the terminal, and save it to a file named filelist.txt
:
ls | tee filelist.txt
This example uses the ls
command to list files, and the output is both displayed on the terminal and saved to filelist.txt
.
Options:
-a
: Appends to the specified files instead of overwriting them.
Example with -a
option:
Appending the output of a command to an existing file:
command | tee -a existingfile.txt
Use Cases:
- Logging the output of a command while displaying it on the screen.
- Creating a record of command execution for auditing purposes.
- Simultaneously feeding input to multiple commands or scripts.
The tee
command provides a flexible way to capture and process command output in Linux.