Introduction to the Linux tee Command
This tutorial will guide you through using the tee
command in Linux, a powerful utility for system administrators. Learn how to simultaneously redirect command output to a file and display it on your terminal. We will explore the purpose, syntax, and practical applications of tee
, including redirecting output and appending to existing files. The tee
command is an invaluable asset for any systemadmin, allowing you to monitor command execution while preserving its output for later analysis.
We'll also cover the -a
option, which enables you to append output to a file instead of overwriting it. This is especially useful for building logs or maintaining a historical record of command outputs.
Understanding the Purpose and Syntax of the Linux tee Command
This section details the function and syntax of the tee
command in Linux. As a systemadmin, understanding tee
is essential. It provides the ability to duplicate command output, sending it to both a file and the standard output (your terminal).
The fundamental syntax for the tee
command is:
tee [options] [file]
The [options]
include:
-a
: Append output to the file, preserving existing content.-i
: Ignore interrupt signals, preventing premature termination.
Consider this example to illustrate tee
's functionality. If you need to execute a command, save the output to a file, and view the output in real-time, tee
is the perfect solution:
$ ls -l | tee output.txt
total 0
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 output.txt
In this case, the output from ls -l
is both saved in output.txt
and displayed on your screen.
Example output:
total 0
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 output.txt
The tee
command shines when you require simultaneous viewing and archival of command output, making it ideal for debugging and monitoring system operations.
Redirecting Command Output to a File and Your Terminal Using tee
This section demonstrates how to use the tee
command to redirect output from a command to both a file and your active terminal window.
First, we'll create a simple text file:
$ echo "This is a sample text file." > sample.txt
Now, use tee
to redirect the output of the cat
command to a file and the terminal:
$ cat sample.txt | tee output.txt
This is a sample text file.
Here, the output of cat sample.txt
is sent to both output.txt
and printed to your screen.
Example output:
This is a sample text file.
The -a
option allows you to append output to an existing file:
$ echo "Adding more content." | tee -a output.txt
Adding more content.
Let's verify the contents of output.txt
:
$ cat output.txt
This is a sample text file.
Adding more content.
As you can see, the new content has been appended to the file.
Appending Output to an Existing File with the tee Command in Linux
This section will guide you on using the tee
command to append output to an existing file. This is a common task for systemadmin when logging activities or building configuration files.
Start by creating a sample file:
$ echo "This is the initial content." > sample.txt
Now, use tee -a
to append the output of the echo
command to sample.txt
:
$ echo "Appending more content." | tee -a sample.txt
Appending more content.
The -a
option instructs tee
to append, rather than overwrite, the file.
Verify the content of sample.txt
:
$ cat sample.txt
This is the initial content.
Appending more content.
The new content has been added to the existing file without deleting the initial content.
You can also append multi-line output using a here-document:
$ cat <<EOF | tee -a sample.txt
> This is the first line.
> This is the second line.
> This is the third line.
> EOF
This is the first line.
This is the second line.
This is the third line.
The output of the cat
command with the here-document is appended to sample.txt
.
Check the file's contents again:
$ cat sample.txt
This is the initial content.
Appending more content.
This is the first line.
This is the second line.
This is the third line.
The multi-line content has been appended successfully.
Summary: Mastering the tee Command for System Administration
In this lab, you gained a solid understanding of the tee
command in Linux, a critical tool for any systemadmin. You learned how to redirect command output to both a file and your terminal simultaneously, ensuring you can monitor command execution while preserving the output. We also covered appending output to existing files using the -a
option. The tee
command is exceptionally useful when you need to view and record command output for future reference, troubleshooting, and system monitoring.