RSYNC 不删除源目录

Saj*_*ikh 30 linux rsync

我正在使用 rsync 从服务器上获取必要的文件,然后在本地拥有这些文件后从服务器中删除这些文件。我正在运行的完整命令如下。

这确实成功删除了源服务器上的文件,但空目录仍然存在。我没有收到任何消息或错误。所有输出正常。也许这是预期的功能。

如何告诉 rsync 清理所有内容,包括目录?

rsync --progress -vrzh --remove-source-files

两端的版本都是 3.0.9。

slh*_*hck 40

手册页甚至说:

--remove-source-files   sender removes synchronized files (non-dirs)
Run Code Online (Sandbox Code Playgroud)

如果要删除源中的空目录,如果仍有文件,请执行以下操作:

find . -depth -type d -empty -delete
Run Code Online (Sandbox Code Playgroud)

如果它只是一个空的源目录,一个rmdir <directory>当然就足够了。

  • 请注意,发出“rm -rf”很容易出现竞争条件,我不鼓励这样做。 (7认同)
  • 是的,这是唯一的解决方案。这是 rsync 的一个愚蠢的缺失功能...... rsync 知道它何时处理目录中的最后一个文件......如果该目录为空,也很容易删除该目录。 (6认同)
  • 不删除顶级空目录的变体:`find some_dir -深度 -type d -empty -not -path some_dir -delete` (3认同)

Mar*_*iae 17

--remove-source-files您观察到的行为正是由 指定的man rsync

--remove-source-files

   This tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side.
Run Code Online (Sandbox Code Playgroud)

没有删除目录的特定命令,正如StackExchangeServerFault 中的这两个讨论清楚地显示的那样。解决方案建议发出两个单独的命令:

 rsync -av --ignore-existing --remove-source-files source/ destination/ && \
 rsync -av --delete `mktemp -d`/ source/ 
Run Code Online (Sandbox Code Playgroud)

这两篇文章中建议的最后一条命令,

 rmdir source/
Run Code Online (Sandbox Code Playgroud)

删除(现已清空)源目录所需的内容在这些帖子中具有这种形式,因为 OP 和答案正在使用 rsync 在同一台机器内移动大量文件。在您的情况下,您必须手动执行此操作。

  • `rsync --delete` 建议很危险,因为它忽略了 rsync 未完成或源中有新文件的可能性。@slhck 下面的 `find` 方法要安全得多。 (6认同)
  • 这是对编写 rsync 的人的愚蠢监督,因为目录也是文件。 (2认同)

Raú*_*udo 6

使用“ rm -rf ”具有固有的竞争条件,您可以即删除刚刚在rsyncrm调用之间创建的文件。

我更喜欢使用:

rsync --remove-source-files -a server:incoming/incoming/ &&

ssh 服务器查找传入 -type d -delete

如果目录不为空,这将不会删除目录。

  • 这个答案错过了“-深度”选项,该选项指示“find”以正确的顺序进行处理。由于此失误,仅包含空目录(可能递归)的目录将不会被删除。 @slhck 的变体是正确的。 (4认同)
  • `rm -rf` 还将删除由于某种原因未传输的文件。 (2认同)