Admin-Ahead Community

General Category => General Discussion => Topic started by: rohitj on November 16, 2013, 05:19:05 pm

Title: Write to more than one file at once with tee command
Post by: rohitj on November 16, 2013, 05:19:05 pm
Write to more than one file at once with tee command

Usually we type the same commands over and over for adding same contents to 3 or 4 files. But there's an easier and faster way of doing it, without redirection and repetitive write operations. We can make use of tee command for this purpose.

tee is a very useful utility that duplicates pipe content.

For example :

---------------
:~# df -h | tee  file1 file2 file3

:~# ls -l
total 16
-rw-r--r-- 1 root root  380 Nov 16 06:43 file1
-rw-r--r-- 1 root root  380 Nov 16 06:43 file2
-rw-r--r-- 1 root root  380 Nov 16 06:43 file3
---------------

As you can see in the screenshots below, all three files were created at the same time and they all contain the same data.

Now, if you wanted to append data to files, that is periodically update them, you would use the -a flag, like this:

========
:~# df -h | tee -a  file1 file2 file3
========

Enjoy   ;)