Slashes and the rsync command

cadams picture cadams · Jul 7, 2015 · Viewed 11.7k times · Source

I am trying to do something along the same lines as what is asked in this question: RSync: How do I synchronize in both directions?

However, what I can't figure out is whether or not I should add slashes to the end of the file path. Basically I'm trying to create an alias command that syncs the contents of two directories that go by the same name but are on two different servers. What I don't want is for one directory to be copied into the other (which I am aware is a possibility depending on how the slashes at the end are done).

What I have currently is:

alias syncDirectories1 = 'rsync -tvur name@host:/Users/me/directory/ /Users/me/directory/'
alias syncDirectories2 = 'rsync -tvur /Users/me/directory/ name@host:/Users/me/directory/'

For what I am trying to accomplish, should there be slashes at the end of both file paths?

Thank you in advance for the help.

Answer

werkritter picture werkritter · Jul 7, 2015

It's described in the rsync(1) manpage.

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 follow‐ ing 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

As to the destination, I don't think it has any major consequences. There is a difference if the source is a file and destination doesn't exist — the command

rsync SRC DEST

will make a copy of SRC called DEST, whereas

rsync SRC DEST/

will create directory DEST and copy the SRC file into it.