tee Command: Tutorial & Examples

Chaining together standard input and output

The tee command in Linux is a command-line utility that is used to chain together standard input and standard output in a way that allows you to write to one or more files while also viewing the output on your terminal. This is typically done by piping the output of a command to tee, which then writes that output to a file and also sends it to the standard output.

The tee command is extremely useful when you want to log the output of a command, but also want to see the output in real time. It's also frequently used in scripts and automations where logging is important.

Common Usage of tee command

The tee command is typically used in conjunction with other commands, using a pipe (|) to direct the output of the preceding command to tee.

Here is a basic example of how to use the tee command:

echo "Hello, World!" | tee hello.txt

In this example, echo outputs the text "Hello, World!", which is then piped to tee. The tee command writes the output to the file hello.txt and also displays it on the terminal.

Parameters of tee command

The tee command has several parameters that modify its behavior:

  • -a: Appends the output to the files rather than overwriting them.
  • -i: Ignores interrupts.

Here is an example of using the -a parameter:

echo "Hello again, World!" | tee -a hello.txt

In this example, the text "Hello again, World!" is added to the end of hello.txt rather than overwriting the existing content.

Using tee command with other commands

The tee command is very powerful when combined with other commands. For example, you can use tee with the ls command to write a list of files in a directory to a file while also displaying the list on the terminal:

ls | tee filelist.txt

Conclusion

The tee command is a powerful tool in your Linux command line toolbox. It allows you to write the output of a command to one or more files while also displaying the output on your terminal. This makes it a valuable tool for logging and troubleshooting. It's definitely a command that every Linux user should become familiar with.

The text above is licensed under CC BY-SA 4.0 CC BY SA