What is tee command in bash?

Overview

The tee command reads the standard input and simultaneously writes to a standard output, into the file(s).

Syntax

command | tee options filenames

Parameters

It takes the following parameters:

  • options:
    • -a: Appends to the file instead of overriding it.
    • --help: Displays the help message and exits.
    • --version: Displays the version and exits.
  • filename(s): Single or multiple file names separated by a space.

Let's take a look at an example.

Example

echo "List of files before executing the tee command"
#list files in present directory
ls
echo "Creating the how_to_ls text file with the tee command"
#create a text file with the tee command
man ls | tee how_to_ls.txt
echo "List of files after executing the tee command"
#list files in present directory
ls

Explanation

In the above code snippet:

  • Line 3: We print the list of files in the present directory before using the tee command with the ls command.
  • Line 7: We display the output of the command man ls and write it into a file how_to_ls.txt using the tee command.
  • Line 11: We print the list of files in the present directory after executing the tee command with the ls command. We can see in the output, how_to_ls.txt is created.

    Free Resources