What if you could watch and save your command output at the same time with one simple trick?
Why tee for splitting output in Linux CLI? - Purpose & Use Cases
Imagine you run a command in the terminal that shows important information. You want to see this information on the screen and also save it to a file for later. So, you run the command twice: once to see it, and once to save it. This wastes time and effort.
Running the command twice is slow and can cause mistakes if the command changes or the output differs. Copying and pasting output manually is tiring and error-prone. You might miss some details or save incomplete data.
The tee command solves this by letting you run the command once, showing the output on the screen and saving it to a file at the same time. It splits the output stream, so you don't have to repeat yourself or risk losing data.
command > output.txt cat output.txt
command | tee output.txt
You can easily watch command results live while keeping a perfect saved copy for review or sharing.
When monitoring a server log, you want to see new entries as they appear but also save them for troubleshooting later. Using tee lets you do both without extra steps.
Running commands twice wastes time and risks errors.
tee splits output to screen and file simultaneously.
This makes saving and viewing output easy and reliable.
Practice
tee command do in Linux?Solution
Step 1: Understand the purpose of
Theteeteecommand takes the output of a command and sends it to both the terminal (screen) and a file at the same time.Step 2: Compare options with this behavior
Only It splits the output of a command to both the screen and a file simultaneously. describes this behavior correctly. Other options describe unrelated actions.Final Answer:
It splits the output of a command to both the screen and a file simultaneously. -> Option AQuick Check:
teesplits output = A [OK]
- Thinking tee merges files
- Confusing tee with file deletion
- Assuming tee changes permissions
tee to save output to a file named log.txt?Solution
Step 1: Recall the pipe usage with tee
The output of a command is piped using|intoteeto save it to a file.Step 2: Check each option's syntax
Only command | tee log.txt uses the correct pipe syntax:command | tee log.txt. Others misuse redirection or order.Final Answer:
command | tee log.txt -> Option CQuick Check:
Pipe output to tee = D [OK]
- Using > before tee instead of pipe
- Placing tee before command
- Confusing redirection with tee syntax
output.txt after running this command?echo "Hello World" | tee output.txt
Solution
Step 1: Understand echo piped to tee
The echo command outputs "Hello World" which is piped to tee. Tee writes it to the file and also outputs it to the screen.Step 2: Determine output locations
Both the screen and the fileoutput.txtwill contain "Hello World".Final Answer:
Screen: Hello World; File output.txt: Hello World -> Option AQuick Check:
tee duplicates output to screen and file = B [OK]
- Assuming tee only writes to file
- Thinking tee suppresses screen output
- Confusing echo output with tee behavior
log.txt instead of overwriting it. Which command fixes the error in this script?ls -l | tee log.txt
Solution
Step 1: Identify the need to append output
The defaultteeoverwrites files. To append, use the-aoption.Step 2: Check correct syntax for appending
ls -l | tee -a log.txt correctly usestee -a log.txtwith pipe fromls -l. Others misuse redirection or order.Final Answer:
ls -l | tee -a log.txt -> Option DQuick Check:
Use -a with tee to append = C [OK]
- Using > redirection with tee incorrectly
- Placing tee before command
- Forgetting the -a option for append
sysinfo.log while still seeing it live on screen. You also want to keep adding to the log without erasing old data. Which command achieves this?Solution
Step 1: Understand the requirement to append and show output
The command must show output on screen and append to the file without overwriting.Step 2: Identify correct tee usage
uname -a | tee -a sysinfo.log usestee -a sysinfo.logwith pipe fromuname -a, which appends and shows output live.Step 3: Eliminate incorrect options
uname -a | tee sysinfo.log overwrites file. Options C and D misuse command order and redirection.Final Answer:
uname -a | tee -a sysinfo.log -> Option BQuick Check:
Pipe output to tee -a to append and show = A [OK]
- Forgetting -a causes overwrite
- Placing tee before command
- Using > redirection incorrectly with tee
Ready to prove it?
Pass the test and this pattern moves unknown
