斜杠和 rsync 命令

Slashes and the rsync command

我正在尝试做一些与这个问题中所问的相同的事情:RSync: How do I synchronize in both directions?

但是,我想不通的是我是否应该在文件路径的末尾添加斜线。基本上我正在尝试创建一个别名命令来同步两个目录的内容,这两个目录具有相同的名称但位于两个不同的服务器上。我不希望将一个目录复制到另一个目录(我知道这是一种可能性,具体取决于末尾的斜线是如何完成的)。

我目前拥有的是:

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

对于我想要完成的,两个文件路径的末尾是否应该有斜杠?

提前感谢您的帮助。

rsync(1) 联机帮助页中对此进行了描述:

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

至于目的地,我不认为它有任何重大影响。如果源是一个文件并且目标不存在,则会有所不同 — 这将创建一个名为 DEST:

SRC 的副本
rsync SRC DEST

,然而,这将创建目录 DEST 并将 SRC 文件复制到其中:

rsync SRC DEST/

保留源上的斜杠,并从目标中删除它们。 像这样:

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

我在 Ubuntu 17.04 上得到的答案与接受的答案不同。目的地 / 似乎没有效果。 我执行了以下四个命令:

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

结论:

  • 一旦src上有/,那么src的内容只会被复制到dest,不管dest有没有斜线。
  • src 上没有斜杠,然后 src 及其内容将被复制到 dest。
  • dest 上的斜杠无效。

我在 Arch Linux 上用 rsync 3.1.3 测试了它,结果如下:

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

方法 3-6 在这种情况下是正确的,与公认的答案相反。