Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Directories

Status
Not open for further replies.

SteveJR

Programmer
Aug 13, 2001
72
GB
Hi All,

Hopefully a very quick answer can be given!

I want to compare the files within 2 directories and only show the files that differ, not the differing content. I have tried diff, using the recursive option, but there doesn't seem to be an option that only reports on the files differing.

Can anyone help?

Thanks,

Steve
 
Something like this maybe?

ls dir1 > dirlist1
ls dir2 > dirlist2

for i in `cat dirlist1`
do
diff $i dirlist2/$i > difffile
if (test -s difffile) then
echo $i
fi
done
 
Thanks for the quick reply mrregan.

The above only works if I am in the directory that I want to compare.

Also, what if that directory has sub-directories and then they have sub-directories etc?

Looking on the net an old version of diff did have a report only differing file option(-q). Strange why it is no-longer available?

Steve
 
I do this using a combination of "sum" and "diff". It's not very clean, but basically...
Code:
BDIR1=$1
BDIR2=$2
HERE=`pwd`
cd $BDIR1
find . -type f -exec sum {} \; >$HERE/dir1.sum
cd $BDIR2
find . -type f -exec sum {} \; >$HERE/dir2.sum
cd $HERE
diff dir1.sum dir2.sum >diff.list

The "exec sum" above will fail on very large lists of files. Also, you may get better results if you first "sort" the sum lists.


"Proof that there is intelligent life in Oregon. Well, Life anyway.
 

Steve,

Did you try the "-q" option of the "diff" command?

 
ZaSter said:
Did you try the "-q" option of the "diff" command?

If you read above, he already considered that, but it doesn't appear to be available on Solaris.

Steve, have you considered using dircmp or perhaps a script around cmp -s?

Annihilannic.
 
motoslide said:
The "exec sum" above will fail on very large lists of files.

It should be fine since find will exec sum once for each individual file. To make it more efficient you could use:

[tt]find . -type f | xargs sum >$HERE/dir1.sum[/tt]

Annihilannic.
 
Thanks for the above replies guys.

Annihilannic - will give your little script a go today and post up the results.

I did try dircmp but it didn't seem to give the results I needed.

Steve
 
Mine won't work on its own, it's just a suggested modification to motoslide's solution.

Annihilannic.
 
Oops!

Thanks for reminding me it was motoslides script.

Steve
 
Hi Guys,

Just tried the script and I apologise up front as my scripting knowledge is not very good at all but it doesn't seem to work because one of the directory names I passing in contains a space in it.

This means that the cd part is not being executed correctly. When I run the script I pass in the params as follows:

. my_script.sh 'dir a' dira

Can someone please post the correct way to pass in the params???

Thanks,

Steve
 
Ok, cracked it. Need to put double quotes around the variable within the script. Should have guessed that :~/

So, script posted by motoslide is now working but the output file diff.list still shows all the differences. All I need to know is the files that differ. The list needs to show the full filename i.e. <dir>/<filename> Can anyone help

Thank you all in advance for the help.

Steve
 
Using quotes like this should help.

Code:
BDIR1="$1"
BDIR2="$2"
HERE="`pwd`"
cd "$BDIR1"
find . -type f -exec sum {} \; >$HERE/dir1.sum
cd "$BDIR2"
find . -type f -exec sum {} \; >$HERE/dir2.sum
cd "$HERE"
diff dir1.sum dir2.sum >diff.list

Note that I've gone back to motoslide's original solution; xargs won't handle filenames with spaces very well on Solaris.

Annihilannic.
 
dircmp is just a ksh script... looking at the code it would appear to be recursive (it does something very similar to motoslide's suggestion), however a quick test I did proved that it doesn't handle filenames with spaces very well either - not good for an OS provided utility!

Although spaces in filenames were never a good idea in any OS in my opinion...

Annihilannic.
 
Thanks Annihilannic.

Maybe the script can be altered to cater for spaces in directories and filenames?

Good point on the spaces - have changed all my directories with spaces in to have an _ instead.

Steve
 
the output file diff.list still shows all the differences. All I need to know is the files that differ

I'm not sure I follow you on this one. You should get a list of files which compare differently (have different content), and files which are in one directory tree, but missing in the other. Are you wanting to exclude those? I'm sure you also don't want to actually keep the SUM values,nor the < and > characters. Those can be pruned away.

Change this:
diff dir1.sum dir2.sum >diff.list

To this:
diff dir1.sum dir2.sum|grep "^<"|awk '{print $3'}

I'm not all the way awake yet, but with some tweaking, you could also prepend BDIR1$ to that ending filename to get the full path to be displayed. And, I know the above line needs help to accomodate files with spaces. These other posters here are much better with awk than I.



"Proof that there is intelligent life in Oregon. Well, Life anyway.
 

Sorry about that, Annihilannic. I was on a Solaris machine and the "diff -q dir1 dir2" worked fine. But it turns out that someone had replaced the Solaris "diff" with the GNU "diff".

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top