Linux 给定两个目录树,如何找出哪些文件因内容不同?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4997693/
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
Given two directory trees, how can I find out which files differ by content?
提问by Mansoor Siddiqui
If I want find the differences between two directory trees, I usually just execute:
如果我想找到两个目录树之间的差异,我通常只执行:
diff -r dir1/ dir2/
This outputs exactly what the differences are between corresponding files. I'm interested in just getting a list of corresponding files whose content differs. I assumed that this would simply be a matter of passing a command line option to diff
, but I couldn't find anything on the man page.
这将准确输出相应文件之间的差异。我只想获取内容不同的相应文件的列表。我认为这只是将命令行选项传递给 的问题diff
,但我在手册页上找不到任何内容。
Any suggestions?
有什么建议?
采纳答案by Mark Loeser
You said Linux, so you luck out (at least it should be available, not sure when it was added):
你说的是 Linux,所以你很幸运(至少它应该可用,不确定它是什么时候添加的):
diff --brief --recursive dir1/ dir2/ # GNU long options
diff -qr dir1/ dir2/ # common short options
Should do what you need.
应该做你需要的。
If you also want to see differences for files that may not exist in either directory:
如果您还想查看可能不存在于任一目录中的文件的差异:
diff --brief --recursive --new-file dir1/ dir2/ # GNU long options
diff -qrN dir1/ dir2/ # common short options
回答by FPC
The command I use is:
我使用的命令是:
diff -qr dir1/ dir2/
It is exactly the same as Mark's :) But his answer bothered me as it uses different typesof flags, and it made me look twice. Using Mark's more verbose flags it would be:
它与 Mark 的完全相同 :) 但是他的回答让我感到困扰,因为它使用了不同类型的标志,这让我看了两遍。使用 Mark 更详细的标志将是:
diff --brief --recursive dir1/ dir2/
I apologise for posting when the other answer is perfectly acceptable. Could not stop myself... working on being less pedantic.
当另一个答案完全可以接受时,我为发布道歉。无法阻止自己......努力减少迂腐。
回答by Alan Porter
I like to use git diff --no-index dir1/ dir2/
, because it can show the differences in color (if you have that option set in your git config) and because it shows all of the differences in a long paged output using "less".
我喜欢使用git diff --no-index dir1/ dir2/
,因为它可以显示颜色差异(如果您在 git 配置中设置了该选项),并且因为它使用“less”显示长分页输出中的所有差异。
回答by todd_dsm
Channel compatriot 'billings' (of freenode/#centos fame) shared his method with me:
频道同胞 'billings'(来自 freenode/#centos 的名气)与我分享了他的方法:
diff -Naur dir1/ dir2
Including the final directory forward slash doesn't matter.
包括最终目录的正斜杠无关紧要。
Also, it appears the -u
option is not available on some older/server versions of diff.
此外,该-u
选项似乎在某些较旧/服务器版本的 diff 上不可用。
The difference in diffs:
diff的区别:
# diff -Nar /tmp/dir1 /tmp/dir2/
diff -Nar /tmp/dir1/file /tmp/dir2/file
28a29
> TEST
# diff -qr /tmp/dir1/ /tmp/dir2/
Files /tmp/dir1/file and /tmp/dir2/file differ
回答by CodeBug
These two commands do basically the thing asked for:
这两个命令基本上完成了要求的事情:
diff --brief --recursive --no-dereference --new-file --no-ignore-file-name-case /dir1 /dir2 > dirdiff_1.txt
rsync --recursive --delete --links --checksum --verbose --dry-run /dir1/ /dir2/ > dirdiff_2.txt
The choice between them depends on the location of dir1 and dir2:
它们之间的选择取决于 dir1 和 dir2 的位置:
When the directories reside on two seperate drives, diff outperforms rsync. But when the two directories compared are on the same drive, rsync is faster. It's because diff puts an almost equal load on both directories in parallel, maximizing load on the two drives.
当目录驻留在两个单独的驱动器上时,diff 优于 rsync。但是当比较的两个目录在同一个驱动器上时,rsync 会更快。这是因为 diff 在两个目录上并行放置几乎相等的负载,从而最大化两个驱动器上的负载。
rsync calculates checksums in large chunks before actually comparing them. That groups the i/o operations in large chunks and leads to a more efficient processing when things take place on a single drive.
rsync 在实际比较它们之前以大块计算校验和。这将 i/o 操作分组为大块,并在单个驱动器上进行处理时实现更高效的处理。
回答by Alexander
Meldis also a great tool for comparing two directories:
Meld也是比较两个目录的好工具:
meld dir1/ dir2/
meld dir1/ dir2/
Meld has many options for comparing files or directories. If two files differ, it's easy to enter file comparison mode and see the exact differences.
Meld 有许多用于比较文件或目录的选项。如果两个文件不同,很容易进入文件比较模式并查看确切的差异。
回答by nh2
Diffoscopeis a great command line based directory diff tool.
Diffoscope是一个很棒的基于命令行的目录差异工具。
I especially like about it that it can diff intofiles:
我特别喜欢它可以区分为文件:
It will recursively unpack archives of many kinds and transform various binary formats into more human readable form to compare them. It can compare two tarballs, ISO images, or PDF just as easily.
它将递归地解压缩多种类型的档案,并将各种二进制格式转换为更易读的形式以进行比较。它可以轻松地比较两个 tarball、ISO 图像或 PDF。
It will not only tell you which files differ, but also how they differ.
它不仅会告诉您哪些文件不同,还会告诉您它们有何不同。
回答by Fábio
You can alsouse Rsync
and find
. For find
:
您还可以使用Rsync
和find
。对于find
:
find $FOLDER -type f | cut -d/ -f2- | sort > /tmp/file_list_$FOLDER
But files with the same names and in the same subfolders, but with different content, will not be shown in the lists.
但是具有相同名称和相同子文件夹但内容不同的文件将不会显示在列表中。
If you are a fan of GUI, you may check Meldthat @Alexandermentioned. It works fine in both windows and linux.
如果你是GUI的球迷,你可以检查合并是@Alexander提及。它在 windows 和 linux 中都可以正常工作。
回答by Javeed Shakeel
To find diff use this command:
要查找差异,请使用以下命令:
diff -qr dir1/ dir2/
-rwill diff all subdirectories too -qtells diff to report only when files differ.
-r也会比较所有子目录 -q告诉 diff 仅在文件不同时报告。
diff --brief dir1/ dir2/
--briefwill show the files that dosent exist in directory.
--brief将显示目录中存在的文件。
Or else
要不然
we can use Meld which will show in graphical window its easy to find the difference.
我们可以使用 Meld,它会在图形窗口中显示,很容易找到差异。
meld dir1/ dir2/
回答by Kickaha
To report differences between dirA and dirB, while also updating/syncing.
报告 dirA 和 dirB 之间的差异,同时更新/同步。
rsync -auv <dirA> <dirB>
rsync -auv <dirA> <dirB>