How To Use the Tee Command in Linux

Updated on February 14, 2025
How To Use the Tee Command in Linux header image

tee is a built-in Linux command that reads from standard input (STDIN) and simultaneously writes it to standard output (STDOUT) and one or more files. It can be seamlessly integrated with other commands through piping, allowing for flexible and customized workflows. The tee command allows you to view the output in real time, unlike redirections, making it ideal for logging or debugging within pipelines.

This article explains how to use the tee command to write and process data using multiple options.

tee Command Syntax

The tee command uses the follows the basic syntax below:

console
$ tee [OPTIONS]... [FILE]...

Within the above command, OPTIONS modify the command behavior while FILE specifies the target file to write the changes to. Below are the supported tee command options.

  • -a or --append: Appends the output to a file instead of overwriting it.
  • -i or --ignore-interrupts: Ignores interrupt signals.
  • -h or --help: Displays the command usage and supported options.
  • -v or --version: Displays the tee version information.

Follow the basic usage steps below to test the tee command on your Linux workstation.

  1. View the tee command basic usage information.

    console
    $ tee --help
    

    Output:

    Usage: tee [OPTION]... [FILE]...  
    Copy standard input to each FILE, and also to standard output.  
    
      -a, --append              append to the given FILEs, do not overwrite  
      -i, --ignore-interrupts   ignore interrupt signals  
      -p                        diagnose errors writing to non pipes  
      --output-error[=MODE]     set behavior on write error. See MODE below  
      --help                    display this help and exit  
      --version                 output version information and exit  
  2. View the tee command version.

    console
    $ tee --version
    

    Your output should be similar to the one below.

    tee (GNU coreutils) 8.30
    Copyright (C) 2018 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
  3. Use the -i option to prevent tee command from terminating when receiving an interrupt signal such as Ctrl + C.

    console
    $ tee -i
    

    The -i option is useful when running long commands to ensure the command is successful without any user interruption. Press Ctrl + Z to terminate the tee command when using the -i (ignore-interrupts) option.

Create Files Using the tee Command

You can use the tee command to create or update files. The -a (append) option updates a file if it exists while running the command without any option overwrites the file if it exists. Follow the steps below to create files using the tee command on your Linux workstation.

  1. Create a new output.txt file using the tee command with a Hello World string.

    console
    $ echo "Hello World" | tee output.txt
    

    Output:

    Hello World
  2. View the output.txt file contents and verify that the Hello World string is available.

    console
    $ cat output.txt
    

    Output:

    Hello World
  3. Create another file merged.txt using the contents of the output.txt file.

    console
    $ cat output.txt | tee merged.txt
    

    Output:

    Hello World

Overwrite Files Using the tee Command

You can use the tee command to overwrite existing files and replace the existing contents. The tee command overwrites existing files by default unless the append option is specified. Follow the steps below to use the tee command to overwrite files.

  1. View the output.txt file and verify that it includes a Hello World string.

    console
    $ cat output.txt
    

    Output:

    Hello World
  2. Overwrite the output.txt file with a new Greetings from Vultr string.

    console
    $ echo "Greetings from Vultr" | tee output.txt
    

    Your output should be similar to the one below:

    Greetings from Vultr
  3. View the output.txt file and verify the change.

    console
    $ cat output.txt
    

    Output:

    Greetings from Vultr

Append Files Using the tee Command

You can use the tee command to append existing files without overwriting the existing content. Use the -a (append) option to append a file with new contents as described in the following steps.

  1. Append the output.txt file with a new Hello From Vultr Docs string.

    console
    $ echo "Hello From Vultr Docs" | tee -a output.txt
    

    Output:

    Hello From Vultr Docs
  2. View the output.txt file contents to verify the change.

    console
    $ cat output.txt
    

    Output:

    Greetings from Vultr
    Hello From Vultr Docs

Write to Multiple Files Using the tee Command

You can use the tee command to write similar content to multiple files simultaneously. This is useful when creating or updating multiple files in different locations while displaying the output in your terminal session.

  1. Write to multiple files using the tee command by specifying the file names directly.

    console
    $ tee <filename> <filename2> <filename3> ....
    
    • For example, create multiple files using the tee command with a The Data Backup Process is Complete string as the common value.

      console
      $ echo "The Data Backup Process is Complete" | tee file1.txt file2.txt
      
    • View the file1.txt and file2.txt contents to verify the change.

      console
      $ cat file1.txt file2.txt
      

      Output:

      The Data Backup Process is Complete
      The Data Backup Process is Complete

      Based on the above output, the tee command created multiple files, file1.txt and file2.txt with the same The Data Backup Process is Complete string. You can use the tee command to create, overwrite, or append data in multiple files.

  2. Write multi-line content to multiple files such as test1.txt, test2.txt using the tee command and Heredoc.

    console
    $ tee test1.txt test2.txt <<EOF
    Backup Report - File 1 and File 2
    2024 Backup Summary
    EOF
    

    The above here document uses the tee command and the <<EOF delimiter to write multi-line content to the test1.txt and test2.txt files.

    • View the test1.txt and test2.txt file content to verify the change.

      console
      $ cat test1.txt test2.txt
      

      Output:

      Backup Report - File 1 and File 2
      2024 Backup Summary
      Backup Report - File 1 and File 2
      2024 Backup Summary

Use the tee Command with Sudo Privileges

Follow the steps below to use the tee command with sudo to perform write specific files that require administrative or elevated user privileges.

  1. Add a new 192.0.2.1 mycustomhost.local entry to the /etc/hosts file using the tee command with sudo privileges.

    console
    $ echo "192.0.2.1 mycustomhost.local" | sudo tee -a /etc/hosts
    

    The above command appends the 192.0.2.1 mycustomhost.local line to the /etc/hosts file which requires sudo user privileges. Without using the sudo command, you will receive a permission denied error because the /etc/hosts file is owned by the root user.

    Output:

    192.0.2.1 mycustomhost.local
  2. View the /etc/hosts file contents to verify the change.

    console
    $ cat /etc/hosts
    

    Your output should be similar to the one below.

    127.0.0.1 localhost
    ..............
    ::1 localhost ip6-localhost ip6-loopback
    ...............
    
    192.0.2.1 mycustomhost.local

Combine the tee Command with Multiple Commands to Filter and Redirect Output

You can use the tee command to redirect and capture command output while displaying it on the terminal at the same time. Combining the tee command with other commands is important in scenarios that involve complex pipelines where you need to save or filter the output of a specific command. Follow the steps below to combine the tee command with other commands to filter and redirect output to one or more files.

  1. Pipe the output of the ls command to the tee command to write the results to a file, such as list.txt.

    console
    $ ls | tee list.txt
    

    The above command lists files in your working directory and uses the tee command to write the results in a list.txt file.

  2. View the list.txt file contents to verify the change.

    console
    $ cat list.txt
    

    Your output should be similar to the one below:

    file1.txt
    file2.txt
    list.txt
    output.txt
    test1.txt
    test2.txt
  3. Use the tee command with multiple pipes to filter and redirect output. For example, run the following command to filter the ls command using tee and grep to redirect output to a final.txt file.

    console
    $ ls | tee intermediate.txt | grep .txt > final.txt
    

    The above command pipes the output of the ls command to the tee command which saves it as intermediate.txt. Then, the grep command filters the intermediate.txt output to sort all .txt files and redirects the output to the final.txt file.

    • View the intermediate.txt file contents to verify the tee command change.

      console
      $ cat intermediate.txt
      
    • View the final.txt file contents to verify the filtered and redirected output.

      console
      $ cat final.txt
      

      Your output should be similar to the one below.

      final.txt
      intermediate.txt
      list.txt
      merged.txt
      output.txt
  4. Use the tee command with advanced piping to filter and sort output. For example, use the tee command with awk and sort to filter the active processes to save the final output to a sorted_process_list.txt file.

    console
    $ ps aux | tee process_list.txt | awk '{print $1, $2}' | sort | tee sorted_process_list.txt
    

    The above command uses advanced piping to write the active process information using the tee command to a process_list.txt file. Then, the awk command extracts the first and second columns (user and process ID), while the sort command uses the default ascending lexicographical order to write the final output to a sorted_process_list.txt using the tee command.

    • View the process_list.txt file and verify that the full process information is available.

      console
      $ cat process_list.txt
      
    • View the sorted_process_list.txt file and verify that a sorted process information list is available.

      console
      $ cat sorted_process_list.txt
      

Conclusion

You have used the tee command in Linux to create and update files. You can use the tee command to perform complex file modification tasks by combining it with multiple tasks. For more information and command options, run the man tee command to view the command manual page.