How to Use the tee Command to Save Terminal Output to a File in Ubuntu

When running commands in the Ubuntu terminal, the output is normally printed directly to your screen and then lost the moment you scroll past it or close the window. If you are installing software, running a diagnostic, or building a project, you often need to save the output to a log file for later review. You can redirect the output to a file using the > operator, but this sends the output only to the file, meaning you can no longer see it on your screen in real-time.

The tee command solves this problem perfectly. It acts like a pipe splitter, reading the output from a command and simultaneously writing it to both your terminal screen and a file. You can watch the output live while knowing it is being safely recorded.

How to Use the Basic tee Command

  1. Open your Ubuntu terminal.
  2. Run any command and pipe its output into tee, followed by the name of the file you want to save to. For example:
    ls -la /var/log | tee output.txt
  3. Press Enter.

The terminal will display the full directory listing on your screen in real-time. Simultaneously, an identical copy of that exact output is silently written to a file called output.txt in your current directory.

You can verify the file was created by running cat output.txt.

How to Append to an Existing File

By default, tee will overwrite the destination file every time you run it. If you are running multiple diagnostic commands and want to save all of their outputs into a single log file, you must use the -a (append) flag.

df -h | tee -a system_report.txt
free -m | tee -a system_report.txt
uptime | tee -a system_report.txt

Running these three commands in sequence will produce a single system_report.txt file containing the output of all three commands, one after the other. Without the -a flag, only the output from the final uptime command would be saved.

How to Write to Multiple Files Simultaneously

The tee command can write to more than one file at the same time. Simply list multiple file names after the command.

echo "Server health check passed" | tee log1.txt log2.txt log3.txt

This single command will display the message on your screen and simultaneously create three identical copies in three separate files.

Using tee with sudo

A common pitfall in the Ubuntu terminal is attempting to write to a system-protected file. A command like echo "new config line" | sudo tee /etc/myconfig.conf is the correct approach.

Many beginners mistakenly try sudo echo "text" > /etc/protected-file, which fails because the > redirect is executed by the current user’s shell (which does not have root permission), not by sudo. Using tee with sudo ensures that the write operation itself runs with elevated privileges:

echo "127.0.0.1 myapp.local" | sudo tee -a /etc/hosts

This correctly appends a new line to the protected /etc/hosts file using root permission, while also displaying the written text on your screen for confirmation.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the best tech tips delivered straight to your inbox.

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