Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Write to more than one file at once with tee command  (Read 2806 times)

0 Members and 1 Guest are viewing this topic.

rohitj

  • Guest
Write to more than one file at once with tee command
« 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   ;)