a street sign with an arrow
Wed Aug 23

Tee Command in Linux with Examples

If you are a Linux user, you may have encountered the tee command in Linux. This command is utilized to read from the standard input and write to both standard output and one or more files. In other words, it enables the user to save and view the output of a command simultaneously.

For instance, if you wish to execute the ls command and save its output to a file named list.txt, you can use the tee command in the following manner:

ls | tee list.txt

This will display the output of ls on the screen and also write it to list.txt.

The tee command is named after a T-shaped pipe fitting that splits a pipe into two outputs. Similarly, the tee command splits the output of a command into two destinations.

How to Use the Tee Command in Linux

The basic syntax of the tee command is as follows:

tee [options] [file]

The options are optional parameters that modify the behavior of the command. The file is the name of the file or files that one wants to write the output to. You can specify more than one file by separating them with spaces.

Some of the common options of the tee command are:

-a or --append: Append the output to the file instead of overwriting it
-i or --ignore-interrupts: Ignore interrupt signals
-p or --output-error: Print error messages if an error occurs while writing
--help: Display help information and exit
--version: Display version information and exit

You can also use man tee or tee --help to see more options and details.

Examples with Different Options

Here are some examples of using the tee command with different options:

To write the output of date to two files called date1.txt and date2.txt, use:

date | tee date1.txt date2.txt

To append the output of who to a file called users.txt, use:

who | tee -a users.txt

To ignore interrupt signals while writing the output of ps to a file called processes.txt, use:

ps | tee -i processes.txt

How to Append Output to a File with the Tee Command

As mentioned earlier, you can use the -a or --append option to append the output to a file instead of overwriting it. This is useful when you want to add more data to an existing file without deleting its previous content.

For example, if you have a file called log.txt that contains some log messages, and they want to append more log messages from another command, they can use:

command | tee -a log.txt

This will append the output of command to log.txt and also display it on the screen.

Example with the -a Option

Here is an example of using the tee command with the -a option:

To append the output of uptime to a file called system.txt, use:

uptime | tee -a system.txt

This will append something like this to system.txt:

10:20:20 up 2:00, 1 user, load average: 0.00, 0.00, 0.00

How to Use the Tee Command with Pipes

One of the most powerful features of the tee command is that it can be used with pipes to redirect the output to multiple files or commands. A pipe is a symbol (|) that connects the output of one command to the input of another command.

For example, if you want to run the ls command and write its output to a file called list.txt and also sort it alphabetically, you can use a pipe and the sort command like this:

ls | tee list.txt | sort

This will display the sorted output of ls on the screen and also write it to list.txt.

You can also use multiple pipes and commands to create complex pipelines. For example, if you want to run the ps command and write its output to a file called processes.txt and also filter it by a keyword and count the number of lines, you can use pipes (|) and the grep and wc commands like this:

ps | tee processes.txt | grep keyword | wc -l

This will display the number of lines that contain the keyword in the output of ps on the screen and also write the output of ps to processes.txt.

Examples with Pipes and Different Commands

Here are some examples of using the tee command with pipes and different commands:

To run the ping command and write its output to a file called ping.txt and also stop it after 5 packets, use:

ping google.com | tee ping.txt | head -5

To run the find command and write its output to a file called find.txt and also compress it with gzip, use:

find . -name "*.txt" | tee find.txt | gzip > find.gz

To run the cat command and write its output to a file called cat.txt and also encrypt it with openssl, use:

cat secret.txt | tee cat.txt | openssl enc -aes-256-cbc -pass pass:password > secret.enc

Difference between tee and >>

The >> operator is a shell redirection operator that appends the output of a command or program to the end of an existing file. If the file does not exist, it creates a new file with the output. The >> operator is similar to the > operator, which overwrites the file instead of appending to it.

Both tee and >> are handy for saving the output of a command or program to a file for later use or analysis. They can also be combined with other commands and operators through pipes (|) to create complex data processing pipelines.

The main difference between tee and >> is that tee can write to multiple files at once, while >> can only write to one file at a time. Tee can also show the output on the terminal, while >> does not.

Another difference is that if the file is not writable by the current user, then using >> will fail before executing the command, while using tee will execute the command but fail in writing to the file. For example, if /etc/file.conf is owned by root, then:

sudo echo "newline" >> /etc/file.conf

will fail with a permission denied error, because the redirection is performed by the shell before running sudo. However,

echo "newline" | sudo tee -a /etc/file.conf

will succeed, because tee is run by sudo with root privileges.

A third difference is that tee can be used as an intermediate command in a pipeline, while >> can only be used at the end. For example, if we want to filter the output of a command (cat) with another command (grep) before writing it to a file, we can use:

cat file.txt | grep "pattern" | tee filtered.txt

but we cannot use:

cat file.txt | grep "pattern" >> filtered.txt

because the redirection will affect the whole pipeline, not just the grep command.

Conclusion

At this point, you have learned about the utilization of the tee command in Linux. The various options available for the tee command have been explored, including appending output to a file and using the command with pipes. This command is a valuable tool that enables users to save and view command output simultaneously, and it can be integrated with other commands to create robust pipelines.

To use the tee command effectively, it is recommended to enclose file names containing spaces or special characters in quotes, utilize absolute or relative paths for file names, verify file permissions before writing to them, and exercise caution when overwriting or appending to existing files. We hope you have enjoyed this article and learned something new.