27

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.

Edward D'Souza
  • 2,453
  • 1
  • 26
  • 24

2 Answers2

22

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.

FatalError
  • 52,695
  • 14
  • 99
  • 116
  • 1
    Unfortunately, this has to work on Mac OS X, and I see from http://stackoverflow.com/questions/752818/why-does-macs-find-not-have-the-option-printf that BSD's find doesn't have a -printf option. So there's apparently no nice to way to make a BSD and GNU compatible command. – Edward D'Souza Apr 12 '12 at 22:26
  • @EdwardD'Souza Consider [MacPorts](http://www.macports.org/) if you want to get GNU tools on OSX. – Tim Pote Apr 12 '12 at 22:43
  • Thanks @FatalError; could you please edit the first part of your command so that it only passes in files in 'a' and 'b', and not all files in the current in the directory? Something like: `( (cd a && find . -type f ! -name *crazy* -print) && (cd b && find . -type f ! -name *crazy* -print) )` – Edward D'Souza Apr 13 '12 at 14:32
  • 1
    @EdwardD'Souza Sure thing. I updated it with what I think should work (once again I'm really only familiar with the GNU flavor of the tools). If that doesn't work, I will update it to your suggestion. – FatalError Apr 13 '12 at 15:13
  • @FatalError I'm unclear as to what "your diff won't include the find command" means. You are saying that files with the name "find" will be excluded because the word "find" is piped to the `-X`? Am I correct? – xdhmoore Apr 03 '13 at 20:05
1

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.

martemiev
  • 418
  • 4
  • 10