Only include files that match a given pattern in a recursive diff

柔情痞子 提交于 2019-12-02 20:33:50

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 -

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.

(I've only tested it with GNU find and diff).

EDIT:

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

That's also assuming that non-GNU diff has -X which I don't know.

I couldn't make it work with -X -, so I used a different approach - let find find the files recursively according to my requirements and let diff compare individual files:

a="/your/dir"
b="/your/other/dir"
for f in $(find $a -name "*crazy*" | grep -oP "^$a/\K.*"); do
  diff -u $a/$f $b/$f
done

Or, in one line:

a="/your/dir" && b="/your/other/dir" && for f in $(find $a -name "*crazy*" | grep -oP "^$a/\K.*"); do diff -u $a/$f $b/$f; done

This grep -oP "^$a/\K.*" is used to extract the path relative to the directory a, i.e. it removes /your/dir/ from /your/dir/and/a/file, to make it and/a/file.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!