QA
Beginner LevelLinux Commands & SysAdmin

How does the tee command write output to both terminal and a file simultaneously?

❓ Interview Prompt: Explain tee command, splitting stdout streams, writing files as root via sudo tee, and append mode (-a).

The 'tee' command reads standard input (stdin) and writes it simultaneously to standard output (terminal display) AND one or more files.

Power Use Case (sudo tee):

Standard redirection ('echo "data" > /etc/file') fails with sudo because redirection is evaluated by the unprivileged shell. Using 'echo "data" | sudo tee /etc/file' writes to root-owned files safely!

Code Implementation Example

bash
# Write command output to stdout AND append to log file (-a)
echo "Server rebooted at $(date)" | tee -a /var/log/reboot.log

# Safely write to root-protected configuration file using sudo tee
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
💡Splitting stdout streams and writing to root-protected files using sudo tee.
🎯 Interview Key Takeaway

Use echo ... | sudo tee -a /etc/file to write output to privileged root files safely.

More Linux Commands & SysAdmin Interview Questions

Beginner LevelLinux Commands & SysAdmin

How do Linux File Permissions (chmod, chown, chgrp) work in Octal and Symbolic notation?

Beginner LevelLinux Commands & SysAdmin

What is the Linux Filesystem Hierarchy Standard (/bin, /etc, /var, /usr, /opt, /tmp)?

Beginner LevelLinux Commands & SysAdmin

What are File Descriptors (0=stdin, 1=stdout, 2=stderr) and Redirection (>, >>, 2>&1)?