仅在递归差异中包含与给定模式匹配的文件 [英] Only include files that match a given pattern in a recursive diff
问题描述
如何对两个目录(a和b)中的文件执行递归diff:
How can you perform a recursive diff of the files in two directories (a and b):
$ diff -r a b
,但仅查看名称与给定模式匹配的文件。例如,使用find命令中可用的相同语法,这看起来像:
but only look at files whose name matches a given pattern. For example, using the same syntax available in the find command, this would look like:
$ diff -r a b -name "*crazy*"
这将显示a和b中具有相同名称和路径的文件之间的差异。
which would show diffs between files with the same name and path in a and b, which have "crazy" in their name.
有效地,我正在寻找diff中可用的--exclude选项的相反内容。
Effectively, I'm looking for the opposite of the --exclude option which is available in diff.
推荐答案
也许这是间接的,但它应该可以工作。您可以使用查找
获取不匹配模式的文件列表,然后排除所有这些文件:
Perhaps this is a bit indirect, but it ought to work. You can use find
to get a list of files that don't match the pattern, and then "exclude" all those files:
find a b -type f ! -name 'crazy' -printf '%f\n' | diff -r a b -X -
-X-
将使 diff
从stdin读取模式,并排除任何匹配的内容。如果您的文件名称中没有 *
或?
之类的有趣字符,这应该可以工作。唯一的缺点是您的差异将不包含 find
命令,因此列出的 diff
命令不是那么有用。
The -X -
will make diff
read the patterns from stdin and exclude anything that matches. This should work provided your files don't have funny chars like *
or ?
in their names. The only downside is that your diff won't include the find
command, so the listed diff
command is not that useful.
(我只在GNU find
和 diff
)。
(I've only tested it with GNU find
and diff
).
编辑:
由于仅非GNU 查找
没有 -printf
, sed
可能是用作替代:
Since only non-GNU find
doesn't have -printf
, sed
could be used as an alternative:
find a b -type f ! -name '*crazy*' -print | sed -e 's|.*/||' | diff -X - -r a b
这还假设非GNU diff
有 -X
我不知道。
That's also assuming that non-GNU diff
has -X
which I don't know.
这篇关于仅在递归差异中包含与给定模式匹配的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!