Linux Command tee and 2 Options

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.

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.

Figure 1. When using the redirection operator (>) without the Linux command tee
Figure 1. When using the redirection operator (>) without the Linux command tee

Basic Usage of the tee Command

The basic syntax for using the tee command is as follows:

<command> | tee <filename>
ShellScript

This 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
ShellScript

When 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.

Figure 2. Using the Linux command tee to both display and save output
Figure 2. Using the Linux command tee to both display and save output

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>
ShellScript

For 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
ShellScript

The result will be saved in both files.

Figure 3. Saving output to multiple files simultaneously with the Linux command tee
Figure 3. Saving output to multiple files simultaneously with the Linux command tee

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>
ShellScript

For instance, to append the output of the df command to disk_usage.txt, use:

df | tee -a disk_usage.txt
ShellScript

In this case, the previous content of disk_usage.txt remains, and the new output is added at the end of the file.

Figure 4. Using the -a option with the Linux command tee to append output to a file
Figure 4. Using the -a option with the Linux command tee to append output to a 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>
ShellScript

For 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
ShellScript

This command continuously monitors the /var/log/syslog file and saves the latest changes to syslog_backup.txt.

Figure 5. Monitoring and saving system logs using the Linux command tee
Figure 5. Monitoring and saving system logs using the Linux command tee

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
ShellScript

In 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
ShellScript

Caution When Using tee

When using the tee command, keep the following in mind:

  1. Overwriting Files: By default, tee overwrites the specified file. Use the -a option to append to the file instead.
  2. 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.
  3. Performance Impact: For commands that output large amounts of data, using tee may impact performance. Be cautious when handling large log files.
  4. sudo Usage: When combining sudo with tee, ensure that sudo is applied directly to tee 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.

References

Leave a Comment