tee
tee
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.
Basic Usage
The basic syntax for the tee
command is:
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.
Examples
Reading from Standard Input
To capture the output of a command and display it on the terminal:
This command lists the files in the current directory (
ls -l
) and simultaneously writes the output to both the terminal and the fileoutput.txt
.
Appending to a File
To append output to an existing file:
The
-a
option appends (tee -a
) the output tooutput.txt
, rather than overwriting it.
Outputting to Multiple Files
To output to multiple files simultaneously:
This command lists the files in the current directory and writes the output to both
file1.txt
andfile2.txt
.
Using tee with sudo
When using tee
with sudo
to write to protected files:
This command uses
sudo
to write the output ofecho
to/path/to/protected/file.txt
, allowing you to write to files that require elevated privileges.
Options
Append to Files
-a
: Append to the given files rather than overwriting them.
Ignore Interrupts
-i
: Ignore interrupt signals (SIGINT
), which allowstee
to continue writing even if interrupted.
Output to Standard Error
-p
: Output to standard error as well as standard output.
Suppress Standard Output
-q
: Suppress standard output.
Practical Use Cases
Monitoring Command 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.
Logging Output
In scripts or automated processes, tee
can log command output to files for later analysis or debugging.
Pipeline Management
Using tee
in complex pipelines helps manage and duplicate output streams effectively, allowing for versatile data processing.
Summary
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.
help
breakdown
Last updated