Capture and view output simultaneously: Which command copies standard output to a file while also echoing it to the terminal at the same time?

Difficulty: Easy

Correct Answer: tee

Explanation:


Introduction / Context:
Sometimes you need to keep a record of command output while still monitoring it on screen. The tee utility solves this by duplicating standard output—writing to both a file and the terminal—making it invaluable for logging and troubleshooting pipelines.



Given Data / Assumptions:

  • The requirement is to save output to a file and display it simultaneously.
  • We are using standard POSIX command-line tools.
  • No special formatting or filtering is required.


Concept / Approach:

The syntax is cmd | tee file. By default, tee overwrites the file; use tee -a file to append. tee acts as a T-junction in a pipeline: it passes input through to standard output and also writes it to the named file(s).



Step-by-Step Solution:

Identify the duplication tool: tee.Form the pipeline: some_command | tee output.logTo append instead of overwrite: some_command | tee -a output.logConfirm that terminal shows live output while the file captures it.


Verification / Alternative check:

Run ls -lR / | tee listing.txt. Observe output on screen and verify listing.txt contains the same data. Re-run with -a to append additional listings without truncating previous content.



Why Other Options Are Wrong:

  • more and cat: Display output but do not simultaneously save to a file.
  • grep: Filters lines by pattern; does not split output to a file and screen.
  • None of the above: Incorrect because tee matches the requirement exactly.


Common Pitfalls:

Forgetting -a when you intend to append; accidentally overwriting existing logs; misunderstanding that tee only captures what is piped into it (stdout), not stderr unless you redirect stderr to stdout (for example, 2>&1).



Final Answer:

tee

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion