Introduction to tee Command in Linux


Introduction

The tee command is a commonly used command in Linux. It is a simple yet powerful command that is used to read standard input, then write it to a file, and also to standard output. tee command takes its name from T-splitter used in plumbing, which splits a stream of water into two streams.

The tee command is a part of coreutils package in Linux, and it is pre-installed on almost all Linux distributions. tee command is useful for a wide variety of tasks, such as creating backups, debugging scripts, and monitoring system logs.

In this article, we will discuss basics of tee command, how it works, and several examples of how to use it.

Basic Syntax

The basic syntax for tee command is as follows −

tee [OPTION]... [FILE]...

The tee command takes one or more options, which are preceded by a dash (-), and one or more filenames. filenames specify files that tee command will write standard input to.

By default, tee will overwrite any existing files. However, you can use -a option to append standard input to end of specified files.

Options

Here are some of most commonly used options for tee command −

-a, --append

This option appends standard input to end of specified files instead of overwriting them.

-i, --ignore-interrupts

This option ignores interrupt signals (such as Ctrl+C) and continues running.

-p, --output-error

This option prevents tee command from exiting on write errors.

--help

This option displays help message for tee command.

--version

This option displays version number of tee command.

Examples

Here are some examples of how to use tee command −

Example 1: Write standard input to a file

The most basic usage of tee command is to write standard input to a file. Here's an example −

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

In this example, we use echo command to write "Hello, world!" to standard output. We then use pipe operator (|) to send standard output to tee command. tee command writes standard input to file hello.txt and also outputs it to standard output.

Example 2: Append standard input to a file

If you want to append standard input to a file instead of overwriting it, you can use -a option. Here's an example −

$ echo "Hello, world again!" | tee -a hello.txt
Hello, world again!
$ cat hello.txt
Hello, world!
Hello, world again!

In this example, we use echo command to write "Hello, world again!" to standard output. We then use tee command with -a option to append standard input to file hello.txt. tee command outputs standard input to standard output, and we use cat command to display contents of file hello.txt.

Example 3: Redirect standard error to a file

The tee command can also be used to redirect standard error to a file. Here's an example −

$ ls /does/not/exist 2>&1 | tee error.log
ls: cannot access '/does/not/exist': No such file or directory
$ cat error.log
ls: cannot access '/does/not/exist': No such file or directory

In this example, we use ls command to list contents of a non-existent directory, which results in an error message being written to standard error. We then use pipe operator to send standard error to tee command, which writes it to file error.log and also outputs it to standard output.

Example 4: Create multiple files at once

The tee command can also be used to create multiple files at once. Here's an example −

$ echo "Hello, Linux!" | tee file1.txt file2.txt file3.txt
Hello, Linux!
$ cat file1.txt
Hello, Linux!
$ cat file2.txt
Hello, Linux!
$ cat file3.txt
Hello, Linux!

In this example, we use echo command to write "Hello, Linux!" to standard output. We then use tee command with multiple filenames separated by spaces to write standard input to files file1.txt, file2.txt, and file3.txt. tee command outputs standard input to standard output, and we use cat command to display contents of each file.

Example 5: Tee and grep

The tee command can be combined with grep to search for a pattern in a file and write matching lines to both a file and standard output. Here's an example −

$ cat example.txt
Hello, Linux!
Welcome to world of open-source.
Linux is a powerful and versatile operating system.
$ cat example.txt | grep Linux | tee filtered.txt
Hello, Linux!
Linux is a powerful and versatile operating system.
$ cat filtered.txt
Hello, Linux!
Linux is a powerful and versatile operating system.

In this example, we use cat command to display contents of file example.txt. We then use pipe operator to send standard output to grep command, which searches for pattern "Linux" in input. grep command outputs matching lines to standard output, and we use tee command to write matching lines to file filtered.txt and also output them to standard output.

Example 6: Tee and awk

The tee command can also be combined with awk to process and manipulate data in a file before writing it to another file. Here's an example −

$ cat data.csv
1,John,Doe
2,Jane,Smith
3,Bob,Johnson
$ cat data.csv | awk -F ',' '{print $2}' | tee names.txt
John
Jane
Bob
$ cat names.txt
John
Jane
Bob

In this example, we use cat command to display contents of file data.csv. We then use pipe operator to send standard output to awk command, which processes data and prints second field of each line (which contains name) to standard output. We use tee command to write output to file names.txt and also output it to standard output.

Example 7: Tee and SSH

The tee command can also be used with SSH to write standard input to a file on a remote server. Here's an example −

$ echo "Hello, Linux!" | ssh user@remotehost "tee hello.txt"
Hello, Linux!
$ ssh user@remotehost "cat hello.txt"
Hello, Linux!

In this example, we use echo command to write "Hello, Linux!" to standard output. We then use pipe operator to send standard output to ssh command, which connects to remote server and runs tee command to write standard input to file hello.txt. We use ssh command again to run cat command on remote server and display contents of file hello.txt.

Conclusion

In conclusion, tee command is a useful and powerful tool in Linux. It allows you to write standard input to one or more files while also outputting it to standard output. tee command can be used for a variety of tasks, such as creating backups, debugging scripts, and monitoring system logs.

In this article, we discussed basic syntax and options of tee command and provided several examples of how to use it. By mastering tee command, you can become a more efficient and productive Linux user.

Updated on: 24-Mar-2023

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements