0
0
Linux CLIscripting~5 mins

tee for splitting output in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to see the output of a command on the screen and save it to a file at the same time. The tee command helps by splitting the output so you don't have to run the command twice or lose the output.
When you want to save the output of a command to a file but also see it live on the screen.
When you run a script and want to keep a log while watching progress.
When debugging and you want to capture output without stopping the flow.
When you want to append output to an existing file while still viewing it.
When you want to share output with another command and save it simultaneously.
Commands
This command prints 'Hello World', shows it on the screen, and saves it to a file named output.txt.
Terminal
echo "Hello World" | tee output.txt
Expected OutputExpected
Hello World
This command reads and shows the content of the file output.txt to confirm the output was saved.
Terminal
cat output.txt
Expected OutputExpected
Hello World
This command adds 'More text' to the end of output.txt while also showing it on the screen. The -a flag means append instead of overwrite.
Terminal
echo "More text" | tee -a output.txt
Expected OutputExpected
More text
-a - Append output to the file instead of overwriting it
Check the file again to see both lines saved after appending.
Terminal
cat output.txt
Expected OutputExpected
Hello World More text
Key Concept

If you remember nothing else from this pattern, remember: tee lets you see command output live and save it to a file at the same time.

Common Mistakes
Using tee without the -a flag when trying to add output to an existing file
This overwrites the file instead of adding to it, losing previous data.
Use tee -a to append output to the file safely.
Trying to use tee without a pipe or input redirection
tee expects input from a pipe or redirection; without it, it waits for manual input.
Always use tee after a command with a pipe or redirect output into it.
Summary
Use tee to split command output so it shows on screen and saves to a file.
Use tee -a to append output to an existing file without overwriting.
Always pipe command output into tee to capture it properly.