Bird
Raised Fist0
Linux CLIscripting~10 mins

tee for splitting output in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - tee for splitting output
Run command producing output
↓
Output stream sent to tee
↓
Write to file
↓
End of process
The command output is sent to tee, which splits it to both a file and the screen simultaneously.
Execution Sample
Linux CLI
echo "Hello World" | tee output.txt
This command prints 'Hello World' on the screen and saves it to output.txt at the same time.
Execution Table
StepActionInputOutput to ScreenOutput to File
1Run echo commandNo inputHello World
2Pipe output to teeHello WorldHello WorldHello World
3tee writes to fileHello WorldHello WorldHello World
4tee outputs to screenHello WorldHello WorldHello World
5Process endsNo inputHello World displayedFile saved
💡 Command finishes after tee writes output to both screen and file.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Output StreamemptyHello WorldHello WorldHello WorldHello World
File output.txtemptyemptyHello WorldHello WorldHello World
Screen outputemptyHello WorldHello WorldHello WorldHello World
Key Moments - 2 Insights
Why do we see the output on the screen even though it is saved to a file?
Because tee duplicates the output stream: it sends the output both to the file and to the screen as shown in execution_table steps 2 to 4.
What happens if the file already exists?
tee overwrites the file by default, replacing its contents with the new output, as implied in step 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output to the screen at step 3?
ANo output
BHello World
CEmpty line
DError message
💡 Hint
Check the 'Output to Screen' column at step 3 in the execution_table.
At which step does tee write the output to the file?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Output to File' column in the execution_table.
If we remove tee from the command, what changes in the variable_tracker?
AOutput to file is empty
BOutput to file remains the same
COutput to screen disappears
DOutput stream becomes empty
💡 Hint
Without tee, output is not saved to file, check 'File output.txt' row in variable_tracker.
Concept Snapshot
tee command duplicates output streams.
Syntax: command | tee filename
Outputs go to both screen and file.
Overwrites file by default.
Use -a to append instead of overwrite.
Full Transcript
The tee command in Linux takes the output of a command and splits it so that it goes to both the screen and a file. For example, echo "Hello World" | tee output.txt prints Hello World on the screen and saves it to output.txt at the same time. The flow starts with the command producing output, which is piped to tee. Tee then writes the output to the file and also sends it to the screen. This way, you can see the output live and keep a copy saved. If the file exists, tee overwrites it unless you use the -a option to append. This is useful when you want to log output but still watch it as it happens.

Practice

(1/5)
1. What does the tee command do in Linux?
easy
A. It splits the output of a command to both the screen and a file simultaneously.
B. It merges multiple files into one.
C. It deletes files after displaying their content.
D. It changes file permissions.

Solution

  1. Step 1: Understand the purpose of tee

    The tee command takes the output of a command and sends it to both the terminal (screen) and a file at the same time.
  2. 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.
  3. Final Answer:

    It splits the output of a command to both the screen and a file simultaneously. -> Option A
  4. Quick Check:

    tee splits output = A [OK]
Hint: Remember: tee shows output and saves it simultaneously [OK]
Common Mistakes:
  • Thinking tee merges files
  • Confusing tee with file deletion
  • Assuming tee changes permissions
2. Which of the following is the correct syntax to use tee to save output to a file named log.txt?
easy
A. tee command > log.txt
B. command tee > log.txt
C. command | tee log.txt
D. command > tee log.txt

Solution

  1. Step 1: Recall the pipe usage with tee

    The output of a command is piped using | into tee to save it to a file.
  2. 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.
  3. Final Answer:

    command | tee log.txt -> Option C
  4. Quick Check:

    Pipe output to tee = D [OK]
Hint: Use pipe (|) before tee to split output [OK]
Common Mistakes:
  • Using > before tee instead of pipe
  • Placing tee before command
  • Confusing redirection with tee syntax
3. What will be the output on the screen and in the file output.txt after running this command?
echo "Hello World" | tee output.txt
medium
A. Screen: Hello World
File output.txt: Hello World
B. Screen: (empty)
File output.txt: Hello World
C. Screen: Hello World
File output.txt: (empty)
D. Screen: (empty)
File output.txt: (empty)

Solution

  1. 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.
  2. Step 2: Determine output locations

    Both the screen and the file output.txt will contain "Hello World".
  3. Final Answer:

    Screen: Hello World; File output.txt: Hello World -> Option A
  4. Quick Check:

    tee duplicates output to screen and file = B [OK]
Hint: tee shows output on screen and saves to file simultaneously [OK]
Common Mistakes:
  • Assuming tee only writes to file
  • Thinking tee suppresses screen output
  • Confusing echo output with tee behavior
4. You want to append output to log.txt instead of overwriting it. Which command fixes the error in this script?
ls -l | tee log.txt
medium
A. ls -l > tee -a log.txt
B. ls -l | tee > log.txt
C. tee -a ls -l log.txt
D. ls -l | tee -a log.txt

Solution

  1. Step 1: Identify the need to append output

    The default tee overwrites files. To append, use the -a option.
  2. Step 2: Check correct syntax for appending

    ls -l | tee -a log.txt correctly uses tee -a log.txt with pipe from ls -l. Others misuse redirection or order.
  3. Final Answer:

    ls -l | tee -a log.txt -> Option D
  4. Quick Check:

    Use -a with tee to append = C [OK]
Hint: Add -a option to tee to append instead of overwrite [OK]
Common Mistakes:
  • Using > redirection with tee incorrectly
  • Placing tee before command
  • Forgetting the -a option for append
5. You want to run a script that outputs system info and save it to 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?
hard
A. uname -a | tee sysinfo.log
B. uname -a | tee -a sysinfo.log
C. tee -a sysinfo.log | uname -a
D. uname -a > tee -a sysinfo.log

Solution

  1. Step 1: Understand the requirement to append and show output

    The command must show output on screen and append to the file without overwriting.
  2. Step 2: Identify correct tee usage

    uname -a | tee -a sysinfo.log uses tee -a sysinfo.log with pipe from uname -a, which appends and shows output live.
  3. Step 3: Eliminate incorrect options

    uname -a | tee sysinfo.log overwrites file. Options C and D misuse command order and redirection.
  4. Final Answer:

    uname -a | tee -a sysinfo.log -> Option B
  5. Quick Check:

    Pipe output to tee -a to append and show = A [OK]
Hint: Use pipe and tee -a to append and display output live [OK]
Common Mistakes:
  • Forgetting -a causes overwrite
  • Placing tee before command
  • Using > redirection incorrectly with tee
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard