Include only files matching this pattern in recursive mode

How can you do recursive diff files in two directories (a and b):

$ diff -rab 

but look only at files whose name matches the given pattern. For example, using the same syntax available in the find command, it would look like this:

 $ diff -rab -name "*crazy*" 

which will show the differences between files with the same name and path in a and b that have "crazy" in their name.

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

+18
unix shell diff
Apr 12 2018-12-12T00:
source share
1 answer

It may be a little indirect, but it should work. You can use find to get a list of files that do not match the pattern, and then "exclude" all of these files:

 find ab -type f ! -name 'crazy' -printf '%f\n' | diff -rab -X - 

-X - make diff read patterns from stdin and exclude anything that matches. This should work if your files don't have funny characters like * or ? . The only drawback is that your diff will not include the find , so the specified diff not so useful.

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

EDIT

Since only non-GNU find does not have -printf , sed can be used as an alternative:

 find ab -type f ! -name '*crazy*' -print | sed -e 's|.*/||' | diff -X - -rab 

It also suggests that non-GNU diff has -X , which I don't know.

+11
Apr 12 2018-12-12T00:
source share



All Articles