Ubuntu – How to diff two folders to multiple patch files

bashcommand linediff()scripts

I'm not much of a unix guy so I'll just go ahead and ask:

I have a following problem – two folders with plenty of subfolders. I need to diff them.

I tried this:

diff -drupN vanila_kernel_3.0.8 my_kernel_3.0.8 > kernel.patch

and that results in a 85mb file… and not really what I want.

I want the result of the diff to be many smaller patches, ideally one for every changed file with the contents of the change. That means I have t ochange the way I use diff, and I need to put it in some sort of a loop… So i tried running this little script

for file in original_308/*.*; do
    diff -dupN "$file" "my_308/${file##*/}" > "$file".patch

But it doesn't work :/

Ideally, I want to have a .patch file for every change, but having patches for files that changed in original would do just fine (as I could filter the newly added files and just copy them over)

Can someone provide me with a decent way to do this please?


Here's something choroba asked me to post

diff -Naur -x '*.o' -x '*.cmd' -x '*.d' original_308/arch/arm/boot/compressed/head.S my_308/arch/arm/boot/compressed/head.S
--- original_308/arch/arm/boot/compressed/head.S    2011-10-25 07:11:12.000000000 +0200
+++ my_308/arch/arm/boot/compressed/head.S  2012-07-04 03:57:25.000000000 +0200
@@ -656,6 +656,8 @@
@       b   __arm6_mmu_cache_off
@       b   __armv3_mmu_cache_flush

So yes, it is the proper output even though gedit/geany cannot open it. UltraEdit does…

Best Answer

You can use the original format that creates the huge diff file which you can subsequently split into the small files. You might be interested in csplit for the splitting, or you can use a scripting language like Perl:

perl -ne 'if(/^diff -drupN (.*) (.*)/) {
              my $f = $1;
              $f =~ s{/}{_}g;
              open $FH, ">", "$f.patch" or die $!;
          } else {
              print {$FH} $_;
          }' kernel.patch

If you need the diff line in each file, add the print after the open.