How to Use the Linux tee Command: A Comprehensive Guide

linux-tee-command

The Linux tee command works similarly to the cat command, with the exception that, on top of writing the stdin to the stdout, it also writes it to a file. It is among the commonly used Linux examples, especially through piping it with other commands to save the output to a file.

Our aim today is to understand the Linux tee command in detail. We will understand its purpose and syntax, and then give practical examples of using it. Let’s begin!

Understanding the Linux tee Command Basics

The Linux tee command is designed to read from the standard input (stdin) and then write to the standard output (stdout) and a file simultaneously.

The tee command is mainly used in conjunction with other commands. However, when used alone, it follows the below syntax.

The table below shows the different options that you can use with the tee command in Linux.

OptionDescription
-aIt is included when you want to append to a file instead of overwriting it.
-iIt is added when you want to ignore interrupt signals.
–versionIt displays the tee command version.
–helpIt displays the help page.

You can think of the tee command as a way of redirecting the output of a command to a file. Other redirection options include using ‘>’ or ‘>>’ but they are different from the tee command.

The ‘>” redirect will overwrite a file while the ‘>>” will append the file. Unlike these redirect operators, the tee command will display the stdin to the stdout. However, the operators will only save the output of the executed command to a file.

Linux tee Command Examples

This section focuses on how to use the tee command and utilizes examples to ensure you quickly master the command. Take a look!

Example 1: Write to a File

The default behavior of the tee command is that it will write the stdin to a file and overwrite existing contents. In this example, we are using the ls command to display the contents of the current directory. Moreover, we’ve piped the output to the tee command to save it to a file.

Note: If the file exists, its contents will be overwritten. However, if it doesn’t exist, a new file will be created.

linux-tee-command-with-ls-command

We then used the Linux less command to open the created file to verify that what we got in the standard output was also written to the file.

linux-less-command

Example 2: Linux tee Command Write to Multiple Files

In the previous example, we’ve written our output to the standard output and a file. Suppose you want to write the output to multiple files. You only need to include the file names of all the files.

For this example, we are displaying the contents of an existing file using the cat command and piping it to the tee command to have the same output written to two files.

linux-tee-command-with-multiple-files

The ls command confirms that our files have been created and we’ve verified their contents using the cat command.

cat-command-open-files

Example 3: Appending to a File

When you add an existing file with your tee command, the output displayed on the stdout will be written to the added file, overwriting its contents. That’s the default behavior of the tee command.

However, if you don’t want to overwrite the file, you can add the -a option to append to it. Below is an example that appends a line using the echo command.

linux-tee-command-with-echo

When we display the file’s contents, we have the added line appearing at the bottom of the file, confirming that we didn’t overwrite the file’s contents.

cat-command-open-a-file

Example 4: Hide Output of the Command

Normally, the tee command will first display the output of the executed command to the standard output before writing it to a file. Suppose you don’t want the output to be displayed to the stdout. Redirect it to /dev/null as in the below example.

Here, we’ve run the pwd command to show the current working directory and append the output to our ‘hello.txt. When we open the file, the last line shows the output of the pwd command but we managed to hide it from displaying in the stdout.

tee-command-hide-output

Example 5: Combining the tee Command with ‘sudo’

Linux files have permissions that dictate what task you can perform on them. Suppose you have a file owned by root and want to write the output of a command to it. You will get a permission denied error. However, there is a way to utilize sudo to write to a file owned by root.

For this example, we have the below file owned by root.

ls-command-long-list

When we try running the tee command to write to the file, we get a permission denied error.

tee-command-permission-denied

To bypass this error, add sudo before the tee command.

That’s it. We’ve managed to write our output to a file owned by root thanks to adding the sudo option.

tee-command-with-sudo

Example 6: Ignoring Interrupts

When running a command, you can press ctrl + c to interrupt it. Suppose you don’t want to encounter such a case when using the Linux tee command. You can add the -i or ‘–ignore-interrupts’ to ensure that even if you press the interrupt key, it won’t stop the command from completing execution.

Here’s an example where we are running the Linux df command to show the disk space available on the filesystem and write it to a file.

When you run an interrupt key, it won’t affect the execution of the df command.

tee-command-ignore-interrupts

Conclusion

The Linux tee command helps with capturing the input from the stdin, displaying it to the stdout, and writing it to a file. There are different ways and options that you can use the tee command, and this post has shared practical examples to help you get started with using the command for different activities.

Found this helpful? - Share it!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top