Slashes and the rsync command

LinuxUnixDirectoryRsync

Linux Problem Overview


I am trying to do something along the same lines as what is asked in this question: https://stackoverflow.com/questions/1602324/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.

Linux Solutions


Solution 1 - Linux

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 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

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 — this will make a copy of SRC called DEST:

rsync SRC DEST

, whereas, this will create directory DEST and copy the SRC file into it:

rsync SRC DEST/

Solution 2 - Linux

I tested it with rsync 3.1.3 on Arch Linux, the results are below:

1. rsync -avPzu test  login@remote:/home/login/test   "test" directory is copied inside of existing "test" on remote (structure is then test/test/...)
2. rsync -avPzu test  login@remote:/home/login/test/  same as above
3. rsync -avPzu test/ login@remote:/home/login/test   content of "test" directory is synchronized with the remote "test" directory
4. rsync -avPzu test/ login@remote:/home/login/test/  same as above
5. rsync -avPzu test  login@remote:/home/login/       same as above
6. rsync -avPzu test  login@remote:/home/login        same as above

The methods 3-6 are the correct ones in this case, contrary to the accepted answer.

Solution 3 - Linux

keep the slashes on the source, and remove them from the destination. Like this:

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

Solution 4 - Linux

I had different results from accepted answer on Ubuntu 17.04. It seems that the destination / does not have an effect. I did the following four commands:

rsync -av src  nslash    # No slashes on either src or dest
rsync -av src  dslash/   # Slash on Dest only  
rsync -av src/ sslash    # Slash on src only 
rsync -av src/ sdslash/  # Slash on both src and dest

Conclusions:

  • once there is a / on the src, then the contents of the src only will be copied over to the dest, regardless off whether the dest has a slash or not.
  • no slash on the src, then the src and its content will be copied to dest.
  • Slashes on dest has no effect.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncadamsView Question on Stackoverflow
Solution 1 - LinuxwerkritterView Answer on Stackoverflow
Solution 2 - LinuxmakView Answer on Stackoverflow
Solution 3 - LinuxNir LevyView Answer on Stackoverflow
Solution 4 - LinuxFawzi MasriView Answer on Stackoverflow