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

When you execute a massive diagnostic command in Linux (like ping or top), the output streams directly to your terminal screen. If you want to save that data to a file for later review, you can use the standard redirect operator (>) to push the data into a text file. However, standard redirection is an absolute dead end; it completely kills the terminal output. The data is silently written to the file, and your screen remains totally blank, meaning you cannot monitor the process in real-time. To mathematically split the data stream—forcing it to print to your screen AND write to a file simultaneously—you must use the tee command.

How the tee Command Works

The tee command is named after a physical “T-pipe” used in plumbing. It takes a single stream of raw data and forcefully splits it into two identical streams. One stream is pushed to Standard Output (your physical monitor), and the second stream is pushed directly into a designated text file.

To use tee, you must pipe (|) the output of your primary command into it.

Imagine you want to ping a remote server to test the connection, and you want to save the results to a file named network_log.txt.

ping 8.8.8.8 | tee network_log.txt

The moment you press Enter, the terminal will instantly begin printing the live ping responses to your screen, exactly as it normally would. Simultaneously, in the background, the tee engine is capturing every single character and permanently writing it into network_log.txt. You get real-time monitoring and a permanent forensic record simultaneously.

Appending to an Existing File

By default, the tee command is highly destructive. If network_log.txt already exists, executing the standard tee command will instantly wipe the file completely clean and overwrite it with the new data.

If you are running a cron job or a recurring script, you do not want to destroy yesterday’s logs. You must instruct the engine to safely append the new data to the absolute bottom of the existing file without touching the older data. You do this by appending the -a (append) flag.

ping 8.8.8.8 | tee -a network_log.txt

This ensures that every time you run the diagnostic test, the new results are safely stacked at the bottom of your master log file, while still streaming the live output directly to your monitor.

Get the best tech tips delivered straight to your inbox.

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