Text and processing

The tee command

Shows output on screen and saves it to files at the same time.

Syntax

comando | tee archivoTry it in the terminal →

Most useful options

-a, --appendAppends to each file instead of overwriting it
-i, --ignore-interruptsIgnores interrupt signals (Ctrl+C) while it writes
-pUses a mode made for pipes. It is equivalent to --output-error=warn-nopipe, its default
--output-error MODOSets what happens on a write error: warn, warn-nopipe, exit or exit-nopipe

Copy-ready examples

$ echo hola | tee nota.txtshows hola on screen and saves it to nota.txt.

Example output

hola
$ echo 'segunda línea' | tee -a nota.txtappends a line to the end of nota.txt without deleting what was there.
$ ls -l | tee listado.txt | wc -lsaves the full listing to listado.txt and counts its lines.
$ echo 'PRUEBA=1' | sudo tee /etc/mi-configwrites to a protected path. It is an administrator target: check the path before pressing Enter, because it overwrites the file.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
tee: noexiste/f.txt: No such file or directoryThe folder where you want to save does not exist. tee does not create it, so it still shows the output but ends with an error.Create the folder first with mkdir -p or fix the path.
tee: .: Is a directoryYou gave a folder as the target. tee writes to files, not to folders.Give a file name, for example tee output.txt.

Typical mistakes

  • Without -a, tee overwrites the file just like the > operator. If you want to keep what was there, use -a.
  • tee does not create folders: if the path does not exist, the output reaches the screen but the file is not written.
  • With sudo, the > operator runs before sudo and fails. That is why you write echo texto | sudo tee archivo.

Differences from similar commands

  • >: sends the output to the file without showing it. tee shows it and saves it at the same time.
  • xargs: turns the input into arguments for another command. tee does not change the output: it only copies it.

Related commands

FAQ

How do I save the output and see it at the same time?

Pipe it into tee: the command shows the output and tee writes it to the file you name.

Can it write to several files at once?

Yes. echo hola | tee a.txt b.txt writes the same content to both files.

Why does tee complain that it is a directory?

Because the target you gave is a folder. Give the name of a file inside it.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course