Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

How can you perform a recursive diff of the files in two directories (a and b):

$ diff -r a b 

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*" 

which would show diffs between files with the same name and path in a and b, which have "crazy" in their name.

Effectively, I'm looking for the opposite of the --exclude option which is available in diff.

like image 334
Edward D'Souza Avatar asked Apr 12 '12 21:04

Edward D'Souza


1 Answers

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.

like image 143
FatalError Avatar answered Sep 24 '22 20:09

FatalError