The tee command in Unix and Linux is used to read from standard input and write to standard output and files simultaneously. It allows you to redirect the output of a command or a series of commands to multiple destinations, such as files or other commands.
The basic syntax for the tee command is:
command | tee [options] [file(s)]command: The command whose output you want to capture and duplicate.options: Optional command-line options to modify the behavior oftee.file(s): The name(s) of the file(s) where you want to redirect the output.
To capture the output of a command and display it on the terminal:
ls -l | tee output.txt- This command lists the files in the current directory (
ls -l) and simultaneously writes the output to both the terminal and the fileoutput.txt.
To append output to an existing file:
echo "Additional line" | tee -a output.txt- The
-aoption appends (tee -a) the output tooutput.txt, rather than overwriting it.
To output to multiple files simultaneously:
ls -l | tee file1.txt file2.txt- This command lists the files in the current directory and writes the output to both
file1.txtandfile2.txt.
When using tee with sudo to write to protected files:
echo "Some content" | sudo tee /path/to/protected/file.txt- This command uses
sudoto write the output ofechoto/path/to/protected/file.txt, allowing you to write to files that require elevated privileges.
-a: Append to the given files rather than overwriting them.
-i: Ignore interrupt signals (SIGINT), which allowsteeto continue writing even if interrupted.
-p: Output to standard error as well as standard output.
-q: Suppress standard output.
When debugging or monitoring the output of a command, tee allows you to observe it on the terminal while also saving it for future reference.
In scripts or automated processes, tee can log command output to files for later analysis or debugging.
Using tee in complex pipelines helps manage and duplicate output streams effectively, allowing for versatile data processing.
The tee command is a useful tool for redirecting standard output to both the terminal and files simultaneously in Unix and Linux systems. It offers flexibility with options for appending, handling interrupts, and managing output streams, making it valuable for various scripting and command-line tasks. Understanding its usage and options can enhance your ability to manage and manipulate command output efficiently.
Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.
-a, --append append to the given FILEs, do not overwrite
-i, --ignore-interrupts ignore interrupt signals
-p diagnose errors writing to non pipes
--output-error[=MODE] set behavior on write error. See MODE below
--help display this help and exit
--version output version information and exit
-a, --append: This option appends to the file instead of overwriting it.
-i, --ignore-eof: This option does not stop at end-of-file (EOF) on standard input.
-h, --help: This option shows this help message.