Splitting Streams with tee

Learn how to split streams with the tee command.

The cat and less commands

To see the output on the screen and send it to a file, we can use the cat or less commands to view the file because when we redirect the output to a file, it no longer displays on the screen. But we can also use the tee command. The tee command gets its name from plumbing; a T-shaped pipe fitting splits the water off in two directions. The tee command takes input and splits the stream to the screen and to a file.

The tee command

Let’s execute the history command and pipe its data to the tee command:

$ history | tee commands.txt

Run the complete code on the terminal below for practice.

echo "Hello" 
echo "This is a sample code"
history | tee commands.txt

Get hands-on with 1400+ tech skills courses.