The command tee in Linux: read from stdin and write in file and stdout

Introduction

In the Linux environment, the ability to handle input and output flows is one of the cornerstones of the command line power. The commandteeis at the heart of this philosophy, as it allows you to read from the standard input (stdin) and at the same time double that flow to both one or several files and to the standard output (stdout). This functionality is essential when you need to keep a record of what happens on screen without interrupting the display in real time.

How does tee work under the hood?

When a process sends data to your stdout,teeacts as a «T» in the pipe: intercept the flow, create a copy and send it to each specified destination. The original remains unchanged, so any subsequent process in the chain continues to receive the same data. In essence,teedoes not consume or transform information; simply replica, which makes it safe to use in any situation where audit or debugging is required.

Basic syntax and default behavior

The most elementary way to invoketeeis:

comando | tee archivo.txt

In this example, the output ofcomandois shown in the terminal and stored inarchivo.txt. If the file already exists,teeit overwrites by default. This overwriting behavior can be changed with the option-a, which will be explained later.

Most commonly used options

  • -aor-approve: instead of truncating the file, add the output at the end, preserving the previous content. Ideal for cumulative looms.
  • -ior-ignore-switches: makesteeignore signs of interruption such as SINGENT (Ctrl + C). This ensures that the log file is closed correctly even if the user cancels the precursor process.
  • -help: shows a short help message with all available options.
  • -version: print the program version installed on the system.

Examples of daily use

The true potential ofteeis revealed in practical situations. Here are some cases of common use:

  • Package installation record:
    sudo apt-get install nginx | tee instalación_nginx.log

    This shows the progress of the on-screen installation and simultaneously saves a complete log for subsequent review.

  • Debugging of scripts:
    ./script_de_prueba.sh | tee depuracion.log

    Each output line of the script is displayed and archived, facilitating the identification of errors without losing the interactivity.

  • System resource monitoring:
    vmstat 1 20 | tee uso_vmstat.txt

    A memory and CPU report is captured for 20 seconds, while it is observed in real time.

  • Sending to multiple destinations:
    comando | tee salida1.txt salida2.txt salida3.txt

    The same flow is written in three different files, useful to create instant backup of the same record.

Combining tee with sudo and other commands

When you need to write on a route that requires root privileges, the combination ofteeandsudois the safest and most elegant solution:

echo "configuración nueva" | sudo tee /etc/miapp/conf.d/nuevo.conf

In this case,echorun with the current user's permissions, butteeis executed bysudo, getting the privileges to write in the protected directory. This pattern avoids having to launch a complete shell withsudo -sand reduces the risk surface.

Another advanced use is to chain severalteein series to create output branches:

comando | tee primera_copia.txt | tee segunda_copia.txt > salida_final.txt

Here, the exit fromcomandois doubled inprimera_copia.txtandsegunda_copia.txtwhile the continuous flow is redirected tosalida_final.txtby the operator>. This technique allows you to create very flexible registration pipelines.

Advantages and limitations

Among the main advantages ofteeThe following are highlighted:

  • Simplicity: a single order achieves simultaneous recording and visualization.
  • Transparency: does not alter the data flow, so it can be used in any position of a pipe.
  • Flexibility: supports multiple files, add mode and signal management.

However, there are some considerations:

  • Howteewrite on the files while reading stdin, if the writing process is slow (e.g. an overloaded disk), it can generate a slight back pressure on the pipe.
  • Do not filter or transform data; for these tasks they should be combined with other tools such asgrep, awkorsed.

In practice, these limitations are rarely an obstacle, andteeremains one of the most reliable utility tools kit for any system manager or developer working in Linux.

Conclusion

The commandteeIt exemplifies Unix's philosophy: do a good thing and do it in a composable way. Its ability to read from the standard input and write simultaneously in files and in the standard output makes it an essential ally to create logs, debug scripts and perform audits without sacrificing interactivity. Domain your basic options (-a, -i) and learn to combine it withsudoand other pipe utilities open up a range of possibilities to automate and monitor systems safely and efficiently. The next time you need to save what you see on screen, remember thatteeHe's there to do it with one command.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish