The Linux command tee
is a useful tool that allows you to display the output of a command on the screen while simultaneously saving it to a file. In this post, we will explore how to use the tee
command, its options, and practical applications.
Table of Contents
What is the Linux Command tee?
The tee
command is typically used in combination with a pipe (|
). It intercepts standard output (terminal output) and saves it to a file while still displaying the results on the terminal. Normally, the output of a command flows to a single destination, but with tee
, you can split the output to multiple destinations.
Understanding Standard Output and Standard Input
To better understand Linux commands, it’s important to grasp the concepts of standard output (stdout) and standard input (stdin). Standard output is the default channel through which a program outputs its results, usually displayed in the terminal. Standard input, on the other hand, is the channel through which a program receives data from the user.
The tee
command intercepts this standard output and directs it to both the terminal and a file. This is particularly useful when you want to log the output while also viewing it in real time.
When to Use the Linux Command tee
In the illustration below, when using the redirection operator (>
) with the ls -la
command to save output to output_ls.txt
, nothing is shown on the screen. You would have to check the file using the cat
command to see what was saved. If you want to both save the output to a file and view it simultaneously, that’s when the tee
command comes in handy.
Basic Usage of the tee Command
The basic syntax for using the tee
command is as follows:
<command> | tee <filename>
ShellScriptThis command saves the output of <command>
to the specified <filename>
while displaying the results in the terminal. For instance, to save the result of the ls
command to output.txt
, you would type:
ls | tee output.txt
ShellScriptWhen you run this command, the result of ls
is displayed on the screen and also saved to the output.txt
file. Note that if the file already exists, it will be overwritten.
Saving Output to Multiple Files
You can use the tee
command to save output to multiple files simultaneously. Simply list each file name, separated by spaces:
<command> | tee <file1> <file2> <file3>
ShellScriptFor example, to save the output of the uname -a
command to both output1.txt
and output2.txt
, you can write:
uname -a | tee output1.txt output2.txt
ShellScriptThe result will be saved in both files.
Key Options
The tee
command provides several options to make it even more useful. Here are a couple of frequently used ones:
-a (append): Add to Existing Files
By default, tee
overwrites the content of the specified file. However, if you want to append new output to an existing file, use the -a
option:
<command> | tee -a <filename>
ShellScriptFor instance, to append the output of the df
command to disk_usage.txt
, use:
df | tee -a disk_usage.txt
ShellScriptIn this case, the previous content of disk_usage.txt
remains, and the new output is added at the end of the file.
-i (ignore-interrupts): Ignore Interrupt Signals
The -i
option tells tee
to ignore interrupt signals like Ctrl + C
. This can be useful when you’re running a long command and want to ensure it continues even if an interruption is accidentally triggered.
<command> | tee -i <filename>
ShellScriptFor example, if you’re running a command that takes a long time to execute, you can use this option to prevent accidental interruption from terminating the process.
Practical Tips for Using the tee Command
The tee
command can be incredibly useful in a variety of scenarios. Here are some practical tips to help you get the most out of it:
Saving Command Output in Real-Time
Using tee
, you can save the output of a command in real-time while viewing it in the terminal. This is especially useful for monitoring logs. For example, to monitor system logs while saving them to a backup file:
tail -f /var/log/syslog | tee syslog_backup.txt
ShellScriptThis command continuously monitors the /var/log/syslog
file and saves the latest changes to syslog_backup.txt
.
Using tee with the sudo Command
When you use sudo
with tee
, you can save command output to a file with root privileges. For example, to save network configuration information:
ifconfig | sudo tee network_info.txt
ShellScriptIn this case, tee
saves the output of the ifconfig
command to network_info.txt
with root access.
Be cautious when using sudo
—if you place sudo
in front of the command but not the tee
command itself, tee
won’t have root privileges. Here’s an example to avoid:
sudo ifconfig | tee network_info.txt
ShellScriptCaution When Using tee
When using the tee
command, keep the following in mind:
- Overwriting Files: By default,
tee
overwrites the specified file. Use the-a
option to append to the file instead. - File Permissions: If you don’t have write permission for the file, an error will occur. In this case, use
sudo
to ensure proper permissions. - Performance Impact: For commands that output large amounts of data, using
tee
may impact performance. Be cautious when handling large log files. - sudo Usage: When combining
sudo
withtee
, ensure thatsudo
is applied directly totee
if you need root permissions for saving the output.
Summary
The Linux command tee
is a powerful tool for saving standard output to both the terminal and a file. While its basic usage is simple, you can use the -a
option to append to existing files or direct output to multiple files simultaneously. Additionally, it’s useful for real-time log monitoring and managing file permissions when combined with sudo
. Whether you’re monitoring system logs or saving command output, the tee
command is an essential utility in Linux.