Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何比较两个目录并列出一个目录唯一而不是另一个目录的文件?
我需要比较两个目录并希望找到不重复的文件。文件名足以完成此任务;我真的不关心内容。
我有许多 iPhoto 库,我希望从中获取独特的文件,以便将它们合并到一个库中。我在包内容中,查看目录,只对原件感兴趣,对修改后的不感兴趣。可能 99%+ 将是重复的,在 20K+ 文件列表中只有少数几个独特的。如果有一个应用程序已经这样做了,那就太好了,但我所看到的一切(应用程序和脚本)都只对提供重复项感兴趣。
您可以使用comm:
comm
comm <( cd path1 ; ls ) <( cd path2 ; ls )
第一列中的文件是path1唯一的,第二列中的文件是path2唯一的,第三列中的文件在两个路径中都是通用的。要抑制前两列,请使用
comm -12 ...
要检查整个子树,请使用find而不是ls,但您需要对输出进行排序:
find
ls
comm <( cd path1 ; find | sort ) <( cd path2 ; find | sort )