Linux 给定两个目录树如何找到哪些文件相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10087571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:42:14  来源:igfitidea点击:

given two directory trees how to find which files are the same?

linuxbashdiff

提问by fazineroso

I am writing a bash script, and I would like to know which files are the same in two directory trees.

我正在编写一个 bash 脚本,我想知道两个目录树中哪些文件是相同的。

It would be the opposite of using diff.

这将与使用差异相反。

Well i found the answer myself. I had tried it before, but I thought it did not work.

好吧,我自己找到了答案。我以前尝试过,但我认为它不起作用。

diff -srq dir1/ dir2/ | grep identical

采纳答案by fazineroso

Well i found the answer myself. I had tried it before, but I thought it did not work.

好吧,我自己找到了答案。我以前尝试过,但我认为它不起作用。

diff -srq dir1/ dir2/ | grep identical

What -srqmeans? From diff --help:

-srq是什么意思?从diff --help

-s  --report-identical-files  Report when two files are the same.
-r  --recursive  Recursively compare any subdirectories found.
-q  --brief  Output only whether files differ.

回答by Todd A. Jacobs

You can use a utility like fdupesor rdfindto list duplicate files. For example:

您可以使用fdupesrdfind等实用程序来列出重复文件。例如:

fdupes --recurse dir1 dir2

You might also want to use the --samelineflag to list all duplicates on the same line, rather than in text blocks, depending on how you want to parse the results.

您可能还想使用该--sameline标志在同一行而不是在文本块中列出所有重复项,具体取决于您希望如何解析结果。