[nSLUG] How to sync two directory trees

Daniel Morrison draker at gmail.com
Wed May 27 15:00:03 ADT 2009

2009/5/27 Mike Spencer <mspencer at tallships.ca>:
> me> Is there an easy way to sync or update two nearly identical directory
> me> trees?
> Okay: rsync or union.
> I had browsed the manpage for rsync and found it roughly as lucid and
> straightforward as the manpage for tar. ;-)

Hatem said:

> rsync -avz --progress --delete machine.com:path/to/source path/to/dest

That's good. My favourite options are:

rsync -SHaxv

-S for handling sparse files correctly, and -H for handling hard links
-a is for 'archive mode', which is very commonly used to avoid having
to specify -rlptgoD (which I just copied and pasted from the man page
-x is to avoid crossing filesystem boundaries, in case that's useful.
-v is for verbose, which gives you stats at the end, but doesn't
produce the amount of screen filling details as '--progress'.
-n is useful to see what will happen with actually doing anything.

One of the most important things to remember is that the trailing
slash is critical:

rsync -SHaxv /path/to/source/ user at machine:/path/to/dest/

means copy the CONTENTS of /path/to/source/ into /path/to/dest/
i.e. /path/to/source/A --> /path/to/dest/B

If you leave off the trailing slash:

rsync -SHaxv /path/to/source user at machine:/path/to/dest/

then you are actually copying the thing named 'source',
i.e.: /path/to/source/A --> /path/to/dest/source/A

You haven't mentioned deleting files as part of your requirements, but
usually keeping a complete copy tree includes deleting files that have
been deleted in the original. If you specify '--delete', that will
happen, but be careful. Example:

rsync -SHaxv --delete /first/set/of/files/ user at host:/backups/
rsync -SHaxv --delete /second/set/of/files/ user at host:/backups/

The second command deletes everything copied by the first command,
because of the '--delete' option. Always copy separate backups into
separate directories.


More information about the nSLUG mailing list