The tee
command is used to read from standard input and write to both standard output and files simultaneously. This allows you to view the output of a command while also saving it to a file. It is commonly used in conjunction with other commands in pipelines.
Initial Example
Using tee
to save the output of a command to a file while still displaying it on the terminal:
echo "Hello, world!" | tee output.txt
TEE Parameters
Parameter | Description |
---|---|
-a, --append | Append the output to the specified files instead of overwriting them. |
-i, --ignore-interrupts | Ignore interrupt signals during execution. |
-p | Operate in a more suitable mode when handling pipes. |
--output-error[=MODE] | Define behavior upon encountering a write error. Options include warn , warn-nopipe , exit , exit-nopipe . |
--help | Display help information and exit. |
--version | Show version information and exit. |
Examples
1. Basic Usage
To display the output and save it to a file:
echo "Sample text" | tee file.txt
2. Append to a File
To append the output to a file instead of overwriting it, use the -a
option:
echo "Additional text" | tee -a file.txt
3. Using tee
with Pipes
To use tee
in a pipeline, saving intermediate output to a file while passing it to the next command:
ls -l | tee file.txt | grep "pattern"
4. Writing to Multiple Files
To write the output to multiple files simultaneously:
echo "Multi-file text" | tee file1.txt file2.txt
5. Combining tee
with sudo
To write to a file that requires elevated permissions, combine tee
with sudo
:
echo "Protected text" | sudo tee /protected/file.txt