Ubuntu – Sync specific folder between two Ubuntu machines when connected to the same LAN

file-sharingnetworkingshared-folderssync

I have two Ubuntu machines, a laptop and a desktop.
What I wish to do is, to create a folder in both machines at paths 'path1' and 'path2'.
I want to set up sync between the two folders whenever they are connected to the same local area network (Desktop through Ethernet and Laptop through WLAN). So for example, if I create a file path1/x, it will also appear in path2 as path2/x.

Is there a way I can get this done? [It's ok if deletion sync doesn't work and I have to manually delete a file from both directories simultaneously]

Best Answer

[Answering my own question as I found a nice way to do this]

Use unison tool developed by Benjamin Pierce at U Penn.

Let us assume you have two directories,

/home/user/Documents/dirA/ and /home/user/Documents/dirB/

To synchronize these two, you may use:

~$unison -ui text /home/user/Documents/dirA/ /home/user/Documents/dirB/

In output, unison will display each and every directory and file that is different in the two directories you have asked to sync. It will recommend to additively synchronize (replicate missing file in both locations) on the initial run, then create and maintain a synchronization tree on your machine, and on subsequent runs it will implement true synchronization (i.e., if you delete a file from .../dirA, it will get deleted from .../dirB as well. You can also compare each and every change and optionally choose to forward or reverse synchronize between the two directories.

Optionally, to launch graphical interface, simply remove the -ui text option from your command, although I find the cli simpler and faster to use.

More on this: Unison tutorial at Unison user documentation.