tee – Read data from standard input and redirect to standard output and files

The tee command in Linux is used to read data from standard input and redirect it to both standard output and one or more files. It allows users to view and store the output of a command simultaneously.

Overview

The basic syntax of the tee command is:

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

This command reads the standard input and writes it to both standard output and one or more files. If no files are specified, tee writes to the standard output only.

Examples

  1. The following command will display the output of ls command on the terminal and store it in a file named list.txt.
ls | tee list.txt
  1. The following command will display the output of ls command on the terminal and store it in two files named list1.txt and list2.txt.
ls | tee list1.txt list2.txt
  1. The following command will append the output of date command to a file named log.txt.
date | tee -a log.txt

Use Cases

  • The tee command is useful when you want to view the output of a command on the terminal and store it in a file at the same time.
  • It is also useful when you want to redirect the output of a command to multiple files.

Options

The following table lists the available options for the tee command:

Option Description
-a, –append Append to the output file(s) instead of overwriting them
-i, –ignore-interrupts Ignore interrupt signals
-p, –preserve Preserve the permissions of the output file(s)
–help Display help information
–version Display version information

Troubleshooting Tips

  • If you are not getting the expected output, check if you have specified the correct file name and path.
  • If you are appending to a file, make sure that you have used the -a option, otherwise, the file will be overwritten.

Notes

  • The tee command is often used in combination with other commands and pipelines to manipulate and analyze data in Linux.
  • The command can be used to create backups of important files and directories.