‘rsync’ command source and destination explained

rsync command is super useful because it could incrementally sync files between locations without the overhead of compressing and transferring. There are heaps of tutorials about how to use it, how very few cover the point that how to sync two folders, which I believe is the most common use.

I’d run rsync -avzP dir1 dir2, but unfortunately, this syncs dir1 under dir2. Some people would then run rsync -avzP dir1 dir2-parent. This could work, but it’s particularly dangerous if you add the --delete option. So how to sync the content of the two folders.

If you run man rsync – not many people would use nowadays, you’d get the very import/useful instructions:

A trailing slash on the source changes this behavior to avoid creating an
additional directory level at the destination. You can think of a trailing /
on a source as meaning “copy the contents of this directory” as opposed to
“copy the directory by name”, but in both cases the attributes of the
containing directory are transferred to the containing directory on the
destination. In other words, each of the following commands copies the files
in the same way, including their setting of the attributes of /dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

The instructions are there like forever, right? But the first command is tricky where you could override the destination directory with the content of foo, so I’d recommend you to always use the second way:

rsync -avP dir1/ dir2

And it works the same way for remote directories.

Role transition to a Frontend Developer

No Comments

No comments yet

Leave a Reply