在 Linux 中的 2 个目录之间查找丢失的文件名

mas*_*r00 3 diff filenames rm synchronization

我正在尝试比较 Linux 中的 2 个目录 ( A / B ),并从 B 中删除 A 中不存在的任何文件。

例如,如果文件 '1.jpg' 存在于目录 B 但不存在于目录 A 中,则需要从 B 中删除它。

我试过使用 diff,但所有文件本质上都不同,所以它似乎不起作用。(它们是不同大小但具有相同 ID 的缩略图)。因此,这只能通过文件名完成,而忽略文件的实际内容。

有人可以阐明如何以最小的努力做到这一点吗?

小智 7

rsync 可以快速轻松地做你想做的事:

rsync --dry-run --verbose --recursive --existing --ignore-existing --delete-after A/ B/
Run Code Online (Sandbox Code Playgroud)

从帮助:

 --existing              skip creating new files on receiver
 --ignore-existing       skip updating files that already exist on receiver
 --delete                delete extraneous files from destination dirs
Run Code Online (Sandbox Code Playgroud)

dry-run在您对建议的结果感到满意后删除该选项,以实际执行删除操作。


手册页对选项有更明确的描述,甚至提到了你的用例:

   --existing, --ignore-non-existing
      This  tells rsync to skip creating files (including directories)
      that do not exist yet on the destination.   If  this  option  is
      combined  with  the  --ignore-existing  option, no files will be
      updated (which can be useful if all you want to do is to  delete
      extraneous files).


   --ignore-existing
      This  tells  rsync  to skip updating files that already exist on
      the destination (this does not ignore  existing  directores,  or
      nothing would get done).  See also --existing.
Run Code Online (Sandbox Code Playgroud)