How to Clone Output to the Screen and a File Using the tee Command in Linux

When you are executing a highly complex diagnostic script or a massive software compilation on a Linux server, you must mathematically split the data payload: you need to see the output live on your terminal screen to monitor for catastrophic errors, but you also need to simultaneously write that exact same data stream to a permanent log file for forensic auditing. Attempting to execute this split manually will drop data packets. To force the Linux kernel to algorithmically clone standard input into two simultaneous streams, you must deploy the tee command.

Executing the Pipeline Clone

The tee command (named mathematically after the T-shaped pipe fitting used in physical plumbing) is a highly specialized multiplexer. It ingests a data stream from standard input, rips the data into two perfect clones, routes Clone A directly to standard output (your screen), and routes Clone B directly into a physical file on the hard drive.

Executing a Standard Tee Sequence

Imagine you want to ping a remote server to test latency, but you also want a permanent log of the ping results in a file named latency_test.log.

To execute the multiplex protocol, open your terminal and type:

ping google.com | tee latency_test.log

The exact millisecond you press Enter, the tee engine intercepts the output from the ping command. You will see the ping results scrolling down your active terminal window in real-time. Simultaneously, the engine is silently writing that exact same data block into the latency_test.log file.

Modifying the Append Vector

By default, if the target file (latency_test.log) already exists, the tee engine will execute a highly destructive overwrite, vaporizing the old data. If you are running a cron job or a recurring script and you want to mathematically preserve the historical data, you must force the engine to append the new data to the bottom of the file.

To execute the append protocol, inject the -a (append) flag:

ping google.com | tee -a latency_test.log

The engine will now flawlessly clone the data stream, writing it to your screen and appending it perfectly to the existing file matrix without overwriting a single byte.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.