Help with diff output

I am running diff between two directories dir1 and dir2.

diff --exclude --recursive --brief -b dir1 dir2

The output of the above command is

Files dir1/java/abc/bcd/abc9991.java and dir2/java/abc/bcd/abc9991.java differ
Files dir1/java/abc/bcd/abc9933.java and dir2/java/abc/bcd/abc9933.java differ
Only in dir2/ora/dpc: g025_abc.dpc
Only in dir2/ora/dpc: gc_cvb_jrp_1650_abc.dpc
Only in dir2/ora/dpc: gc_k_jrp_502901690_abc.dpc
Files dir1/ora/ps/abc_bcda640921_bcd.sp and dir2/ora/ps/abc_bcda640921_bcd.sp differ
Files dir1/ora/ps/cvg_fgh640931_drv_msd.sp and dir2/ora/sp/cvg_fgh640931_drv_msd.sp differ

Want to extract file names from the diff output based on file extensions(have over 6 extensions in the subdirectories .java,.dpc,.cvg, and so on)

and delete all the files in dir2 except these in the diff output.

example: from the above diff output I want to extract files

abc9991.java
gc_cvb_jrp_1650_abc.dpc
gc_k_jrp_502901690_abc.dpc
abc_bcda640921_bcd.sp
cvg_fgh640931_drv_msd.sp   from dir2 and delete rest of the files from dir2.

Tried using the cut command but too many sub directories inside and each time the position has to change.

Why are abc9933.java and g025_abc.dpc excluded?

example: from the above diff output I want to extract files

abc9991.java
abc9933.java
g025_abc.dpc
gc_cvb_jrp_1650_abc.dpc
gc_k_jrp_502901690_abc.dpc
abc_bcda640921_bcd.sp
cvg_fgh640931_drv_msd.sp

from dir2 and delete rest of the files.
.

---------- Post updated at 04:35 PM ---------- Previous update was at 04:34 PM ----------

@CarloM: My bad I missed them

The aim being to delete duplicate files from dir2 and sub-directories?

Correct. I only need newer and changed files in dir2.

If your diff supports -s (report identical files):

diff -s --exclude --recursive --brief -b dir1 dir2 | awk '/identical$/ {print $4}' | xargs echo rm

echo is just for error checking, remove it if the output looks right.

EDIT: If not then you could do something a bit more complicated:

diff <(diff --exclude --recursive --brief -b dir1 dir2 | awk '/^Only/ {printf "%s/%s\n", $3, $5} /differ$/ {print $4}' FS=" |:" | sort) <(find dir2 -type f|sort) | awk '{print $2}' | xargs echo rm

Tried this initially. Did not work. The output was rm.

diff -s --exclude --recursive --brief -b dir1 dir2 | awk '/identical$/ {print $4}' | xargs echo rm

Then tried this it showed everything in dir2 as different and when I put the rm command at the end it deleted all the files in dir2.

diff <(diff --exclude --recursive --brief -b dir1 dir2 | awk '/^Only/ {printf "%s/%s\n", $3, $5} /differ$/ {print $4}' FS=" |:" | sort) <(find dir2 -type f|sort) | awk '{print $2}' | xargs echo rm

What output do you get from the 'same files' diff?

diff -s --exclude --recursive --brief -b dir1 dir2 | head -10
diff -s --exclude --recursive --brief -b dir1 dir2 | head -10
Common subdirectories: dir1/aix and dir2/aix
Common subdirectories: dir1/envsql and dir2/envsql
Common subdirectories: dir1/java and dir2/java
Common subdirectories: dir1/ora and dir2/ora

Apparently there's nothing to delete, so xargs is getting a blank input (which is something I didn't consider).

Change the xargs call to:

xargs -r echo rm

and it should get rid of the error for this case.

I see no files being deleted. There are many files that are similar in dir1 and dir2 and none of them got deleted.

Attached is the zip I created for your reference. It has two directories dir_1 and dir_2. If a compare is run between dir_1 and dir_2 , the only files that should be present in dir_2 after the compare should be the files inside dir_2/ora/seq/

What is the --exclude for? With no parameters that appears to be excluding everything, at least on my diff (GNU diffutils 3.3).

Without that it works as expected:

$ diff -s --recursive -b dir_1 dir_2
Files dir_1/java/abc/test/Constants.java and dir_2/java/abc/test/Constants.java are identical
Files dir_1/java/abc/test/constants_1.txt and dir_2/java/abc/test/constants_1.txt are identical
Files dir_1/java/abc/test/delete.java and dir_2/java/abc/test/delete.java are identical
Files dir_1/java/abc/test/test.java and dir_2/java/abc/test/test.java are identical
Only in dir_1/ora: java
Only in dir_2/ora: seq
$ diff -s --recursive -b dir_1 dir_2 | awk '/identical$/ {print $4}' | xargs echo rm
rm dir_2/java/abc/test/Constants.java dir_2/java/abc/test/constants_1.txt dir_2/java/abc/test/delete.java dir_2/java/abc/test/test.java