tee command

Other topics

Remarks:

If a FILE is specified as a dash ("-"), tee writes again to standard output.

Write output to stdout, and also to a file

The following command displays output only on the screen (stdout).

$ ls 

The following command writes the output only to the file and not to the screen.

$ ls > file

The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.

$ ls | tee file

Write output from the middle of a pipe chain to a file and pass it back to the pipe

You can also use tee command to store the output of a command in a file and redirect the same output to another command.

The following command will write current crontab entries to a file crontab-backup.txt and pass the crontab entries to sed command, which will do the substituion. After the substitution, it will be added as a new cron job.

$ crontab -l | tee crontab-backup.txt | sed 's/old/new/' | crontab –

write the output to multiple files

You can pipe your output to multiple files (including your terminal) by using tee like this:

$ ls | tee file1 file2 file3

Instruct tee command to append to the file

By default tee command overwrites the file. You can instruct tee to append to the file using the –a option as shown below.

$ ls | tee –a file

Syntax:

  • tee [OPTION]... [FILE]...

Parameters:

OptionsDescription
-a, --appendAppend to the given FILEs. Do not overwrite.
-i, --ignore-interruptsIgnore interrupt signals.
--helpDisplay a help message, and exit.
--versionDisplay version information, and exit.

Contributors

Topic Id: 10588

Example Ids: 31779,31780,31781,31782

This site is not affiliated with any of the contributors.