How to Use the Linux tee Command With Useful Examples

How to Use the Linux tee Command With Useful Examples

Navigating Linux commands can be intimidating, but the more you learn, the easier it gets. The Linux tee command is a versatile tool that streamlines your workflow by redirecting standard output to multiple destinations simultaneously.

It’s for more than just advanced users – even beginners can harness its essential functions. In this guide, we’ll explain the tee command in Linux, explore its syntax, and provide practical examples to help you master this important utility. Let’s get started.

Linux tee Command Syntax

The tee command reads standard input and writes it to the terminal and one or more files. Here’s the basic syntax:

command | tee [options] [file]
  • command – this is the initial command you execute, the output you wish to redirect. Examples include shell commands like ls, cat, and uptime.
  • | – the pipe character redirects the output of the preceding command (command) to the following command (tee).
  • tee – this command reads from standard input and writes to standard output and specified files.
  • [options] – here, you can specify flags to change how tee behaves. For example, the -a flag appends output to a file instead of overwriting it.
  • [file] this is where you name the file(s) to save the standard output. You can specify multiple files, separating them with spaces.

For instance, to save the output of the echo command to a root-owned file named hello.txt, execute:

echo "Hello, world!" | tee hello.txt

This command shows Hello, world! on your Linux terminal and saves it to this TXT file.

How to Use the Linux tee Command

Now that you know the basic syntax, let’s use the Linux tee command. If you’re on a virtual private server (VPS), access your server’s command line via SSH.

To log in, get your SSH details from your VPS provider and use an SSH client like PuTTY.

How to Save Command Output to a File

Saving command output is simple with the Linux tee command. To direct standard output to a file, follow this format:

command | tee output.txt

For example, to capture the output of the ls command, run:

ls | tee directory-listing.txt

This will show the current directory listing on your terminal and write it to directory-listing.txt, which is useful for tasks like logging system data.

To write to multiple files at once, use:

ls | tee file1.txt file2.txt

How to Append to an Existing File

The Linux tee command’s default behavior is to overwrite the content of the specified file. To append output without replacing what’s already there, use the -a flag like this:

command | tee -a existing-file.txt

For example, to append system uptime to an existing log file called system-log.txt without deleting prior entries, run:

uptime | tee -a system-log.txt

This keeps your previous entries while adding new data. It’s ideal for ongoing updates to standard output files.

Suppose you’ve already saved a list of installed packages in installed-packages.txt. To add new entries when you install new packages, execute:

dpkg --get-selections | tee -a installed-packages.txt

This tee Linux command appends the current list of installed packages to the existing file, preserving the data captured by the previous command.

How to Combine the tee Command With Piping

The tee command in Linux becomes even more useful when combined with piping and other shell commands. This allows you to filter and manipulate standard input before writing it to a file or displaying it on the terminal. Here’s how you can structure the command:

command1 | tee output1.txt | command2 | tee output2.txt

For instance, to search for a specific error message in a log file and save that filtered output, execute:

cat logfile.txt | grep "ERROR" | tee error-log.txt

The tee command reads standard input from logfile.txt, filters lines containing ERROR, and writes those lines to both the terminal and error-log.txt.

Additionally, the Linux tee command can be interrupted by SIGINT or SIGTERM signals unless managed at the shell level to ignore interrupts. To address this, you can use the trap command before executing the tee command, as shown:

trap "echo 'Interrupt signal caught, exiting.'; exit" SIGINT SIGTERM
tail -f continuously-updating-log.txt | tee -a captured-log.txt

In this example, the trap command is prepared to catch interrupt signals. The tail -f command reads the continuously updating log and tee -a appends this information to captured-log.txt.

Linux tee Command Best Practices

When using tee commands, follow the best practices to optimize your workflow. These include simplifying Linux commands with AI and managing both standard output and standard error in Linux.

Simplify tee Command Management with Hostinger VPS AI Assistant

Managing tasks with Linux command-line tools can be challenging, especially for those new to Linux system administration. Fortunately, for Hostinger VPS hosting customers, our VPS AI Assistant simplifies this process.

This AI-powered tool provides precise and timely guidance for setting up and managing your tee or other command tasks. So, there’s no need to sift through manuals or FAQs, as you’ll receive step-by-step instructions.

For instance, to set up tee command automation, the AI Assistant will generate the required commands for you. Copy and paste the Linux tee examples, and you’re set.

Here’s how to use the Hostinger VPS AI Assistant:

  1. Log in to your Hostinger account.
  2. Locate the VPS dashboard and select your server.
  3. On the left panel, navigate to Help → AI Assistant.
Selecting the AI Assistant menu in the left sidebar of hPanel's VPS dashboard
  1. Type in your question about tee or other commands.
  2. The AI Assistant will provide you with some ready-to-use Linux tee command examples.
Demonstrating the AI Assistant usage to provide the Linux tee command examples

Streamline Output With tee Flags

Optimizing standard output management in Linux is easier with tee command flags. The -a or –append flag is useful, allowing you to add new data to an existing file without erasing what’s already there.

For example, if you’re regularly logging system performance data, use the -a flag to showcase appending output in Linux. Execute the command as follows:

command | tee -a output-file.txt

This method is convenient for tasks requiring ongoing data collection. The tee command, at the same time, appends to existing logs in multiple files.

Redirect Standard Error (stderr)

In Linux, standard output (stdout) and standard error (stderr) are distinct streams. While stdout manages regular output to a file, stderr handles error messages. Capturing both is vital for thorough system diagnostics.

To capture both stdout and stderr, execute the command as follows:

command 2>&1 | tee both-output.txt

This logs both standard and error messages in both-output.txt for later analysis.

For instance, to diagnose error-writing issues to a specific log file, run:

some_command 2>&1 | tee error-log.txt

This technique allows for in-depth troubleshooting, capturing the write error and standard outputs for analysis.

Create Multiple Copies Simultaneously

The Linux tee command excels when you need to write to multiple files, as it simultaneously copies the same output into different locations. To do this, use the following syntax:

command | tee file1.txt file2.txt file3.txt

This feature is handy for logging network activity in given files and directories, streamlining the task while ensuring data consistency.

Combine tee With grep

Pairing tee with the grep command lets you filter specific lines from a data stream and save them. This is one of the Linux command-line tricks useful for tasks like sorting through log files. The syntax to achieve this is as follows:

command | grep 'pattern' | tee filtered-output.txt

For instance, to extract all lines containing ERROR from a system log, run the following command:

cat system-log.txt | grep 'ERROR' | tee error-lines.txt

This setup lets you display the filtered ERROR lines on the terminal and simultaneously save them to error-lines.txt, streamlining your logging process.

Conclusion

The Linux tee command is a powerful tool that lets you display data on the terminal and save it to one or more files. This guide has explored its primary usage, including redirecting output in Linux, using tee for logging, and pairing it with grep.

Becoming proficient in these features will not only boost your Linux command-line skills but also streamline daily operations.

Whether using the tee command for real-time monitoring or to diagnose errors during debugging, mastering tee is invaluable for system administration and to level up your Linux management skills.

Author
The author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.