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.
#tee#Redirection#sudo#Beginner
Back to all Linux Commands & SysAdmin Questions→