tee Command In Linux - Linux Commands Guide

sohail
sohail

Table of Contents

​Today, we are going to go over the Linux tee command. There isn’t much to this command so this guide will be brief. You will find some use for it at some point.

Linux tee Command Usage and Examples

tee command (as in a T-splitter in plumbing) takes content from standard input, displays it, and writes it to one or more files. It is useful if you need to write things to several files in one go. You have the choice to either overwrite the contents of the file or append new content to the end.

tee is very simple and handy if you need something like this. It should be installed on all Linux systems as part of the GNU Coreutils package.​There is a short man page about tee command in Linux.

How To Use tee command

​All you really need to do is specify which files to write to, and if you like, append the input to the end of each file with the ‘-a‘ parameter.

$ tee output.txt # Default behavior is to overwrite the content. 
$ tee -a output.txt # Adds to the file instead. 
$ tee -a copy1.txt copy2.txt output.txt # Write to multiple files.

Input From Files

​By default, you would type what you wanted to save in the terminal. Standard input can also come from the use of pipes. Pipes serve as a method for parsing output from one command to another. Here is an example:

$ cat input.txt | tee output.txt

​What will happen is that whatever is in the input file (output from ‘cat’) will be displayed on the screen, and then written to the file, or files, specified. After writing the output to file, ‘tee‘ will exit.

Error Output

If you look at the man page for ‘tee‘, you will find options pertaining to error reporting. There are two options, ‘-p‘ for diagnosing errors, and ‘--output-error‘ for controlling what happens when an error occurs. One of two things can happen: either the error diagnosis is written in the output (‘warn’), or the program quits (‘error’). In either case, you can either have the output written to pipes or not (default).​There is also an option to ignore any interrupt signals (Ctrl-C to exit a process, ‘-i‘).

tee command Examples

I will quickly provide a couple of little things you could do with tee. These examples make use of the output from other programs.​You could concatenate content from multiple files, and process them through tee.

$ cat file22.txt oldfile3.txt | tee newfile.txt

​Redirect stderr output in a script to files through a pipe using tee.

$ ls nonexistent 2>&1 | tee stderr.log

Conclusion

​tee is a small Linux command that takes input from either standard input or content from a file, to put into one or more files. It comes with the Linux system as part of the ‘coreutils’ toolset. It is very simple and easy to use and will come in handy during scripting. As demonstrated, it can be rather useful, when used with other tools with the use of pipes.

linux command guideLinux toolsUncategorizedtutorial