Keep two directories synced in ubuntu in real time
<p>My intention is to keep two directories (say <code>dir1</code> &amp; <code>dir2</code>) synced. So that whenever there is a change in the content of the <code>dir1</code> (can be addition or deletion of new file or directory or modifying the content of a file in the directory) then the change should be propagated to <code>dir2</code> and vice versa. </p>

<p>The naive way I can think of doing this is to run <code>rsync</code> periodically via <code>cron</code> in both the machines. But there are fallacies in this approach:- </p>

<ol>
<li>It might happen that the previous <code>rsysnc</code> is not complete and the <code>cron</code> executed <code>rsync</code> once more while the previous <code>rysnc</code> is still going on.</li>
<li>A new file is added in <code>dir1</code> and before <code>rsync</code> ran on <code>dir2</code> <code>rsync</code> on <code>dir1</code> ran then newly added file might be deleted from <code>dir1</code> since it is not present in <code>dir2</code></li>
<li>Also this is not real time.</li>
</ol>

<p>Can some suggest some better way of doing this?</p>