Compare two folders and file content

Hi,

I am having two folders where i need to compare the content of files and also to know if any new files been added and redirect the difference output in respective filename logs. For e.g.:

Directory D1:
f1
f2
f3

Directory D2:
f1
f2
f3
f4

i Need to compare the directories D1 and D2 and write it respective file log of there are any difference and if any additional file added write in another log file

Output content in the log file

f1.log - There is a difference in the f1 and f2, please find the below difference:
......
f2.log - No difference found
f3.log - No difference found
Additional.log - There is additional file in D2 with filename as f4

I tried using below code:


diff -arq D1 D2

And what did it do?

I am able to get only the outputs as showing there is file difference and additional file present in D2 i need to print the difference like how it can be achieved in respective log

-q stops it from printing a lot of the information you want, remove that.

That even with diff -r i can able to achieve but i need to right the log so that it would be tracked

Show the output you have and I can try and convert it into the format you want.

I tried something like this

cd D1
for i in *
do
diff $i D2/$i >> $i.log
done

But couldn't able to work as my desired output

diff -r already does what you want, it is recursive.

Show the output you have and I can try and convert it into the format you want.

Hi ,

I am not able tp print in respective log files

Add >> logfile to the end of the diff command.

You are creating log files in the same directory that you are comparing.
Redirect to another path so the log files are not included as part of the `diff'

Is there any way where i can able to print that additional file found in common log file like the desired output i mentioned

diff already reports 'only in' files:

$ mkdir a b
$ echo asdf > a/qwer
$ diff -r a b
Only in a: qwer

$

It's just a matter of changing diff's output into what you want.

I'd been hoping for the slightest, teeniest bit of effort on your part in that instead of just asking us to do everything for you, over and over.

diff -rs a b |
	awk -F"[: \t]" '
	/^diff/ { print "difference between " $3, $4 ; getline ; getline
	while(/^[<>-]/) { print ; if(getline != 1) break } }
	/^Only in/ { print "There is additional file in "$3" with filename as "$NF } 
	/identical$/ { N=split($2, A, "/"); print A[N] " - no difference found" }'

Thanks but is there any way to show the entire difference as i can able to get the diff of first line only using getline

It does show the entire difference. If it's not what you want, please show what it does, then show what you'd rather have it do.

Hi,

Yeah its worked but is there a way to redirect the output to respective log file

f1.log - There is a difference in the f1 and f2, please find the below difference:
......
f2.log - No difference found
f3.log - No difference found
Additional.log - There is additional file in D2 with filename as f4
diff -rs a b |
	awk -F"[: \t]" '
	/^diff/ { print "difference between " $3, $4 ; getline ; getline
	while(/^[<>-]/) { print ; if(getline != 1) break } }
	/^Only in/ { print "There is additional file in "$3" with filename as "$NF } 
	/identical$/ { N=split($2, A, "/"); print A[N] " - no difference found" }' >$3.log

So f1.log isn't the file being checked, it's the file data is printed to?

Does it belong in the first or second dir, or elsewhere?

This will be redirected to somewhere else for e.g. like to check the log of difference between two directories and print the desired output in respective file logs.

If f1 from D1 and f1 D2 are identical print the output in f1.log as "No difference found"
If difference found f1.log should have output as "Please find the difference below:
show the difference

"
If there is an additional file then print the output log as additional.log as "Please find the additional file below:
There is additional file in D2 with filename as f4"

But what if there's two different files of the same name? like dir1/a/f1 and dir1/b/f1

In that case print the output in same f1.log with which directory the diff is there