Tuesday, May 10, 2016

tee command examples in linux?


In computing, tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input. It is primarily used in conjunction with pipes and filters.




Image result for tee command in unix



Example 1: 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

Example 2: Write the output to two commands
You can also use tee command to store the output of a command to a file and redirect the same output as an input to another command.
The following command will take a backup of the crontab entries, and pass the crontab entries as an input 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  -e

Misc Tee Command Operations

By default tee command overwrites the file. You can instruct tee command to append to the file using the option –a as shown below.
$ ls | tee –a file
You can also write the output to multiple files as shown below.
$ ls | tee file1 file2 file3


No comments:

Post a Comment