compare directory contents plus copy differences?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
scsijon
Posts: 1596
Joined: Thu 24 May 2007, 03:59
Location: the australian mallee
Contact:

compare directory contents plus copy differences?

#1 Post by scsijon »

HI, coding problem.

I have

Code: Select all

diff <(cd directory1 && find . | sort) <(cd directory2 && find . | sort)
Which compares two directories and tells me what's missing.
Currently I change it to

Code: Select all

diff <(cd directory1 && find . | sort) <(cd directory2 && find . | sort) >difffile
To give me a file listing the missing files.

What I want to do is take this one step further and after creating another pair of directories called directory1-missing-directory2 and directory2- missing-directory1 have these new directories populated with a copy of whats missing.

so I think it becomes something like

Code: Select all

#!/bin/sh
#involk as cmpsrt directory1 directory2
#outputs directories dir1-missing-dir2 and dir2-missing-dir1
#containing the contents missing in both ways
#
mkdir $1-missing-$2 
mkdir $2-missing-$1
diff <(cd $1 && find . | sort) <(cd $2 && find . | sort)
???
But I don't know what to put in the ??? part?

Or is there a better way?

Can I have some help please.

thanks in advance.

User avatar
6502coder
Posts: 677
Joined: Mon 23 Mar 2009, 18:07
Location: Western United States

#2 Post by 6502coder »

Here's a crude script that illustrates a solution:

Code: Select all

#!/bin/sh
# arguments are dir1 and dir2, relative to pwd
if [ $# != 2 ]; then
    echo "needs 2 args"
    exit 1
fi
D=/tmp/diff_$$
mkdir $D
list1=$D/list1
list2=$D/list2
listall=$D/listall
listdiff=$D/listdiff
list1not2=$D/list1not2
list2not1=$D/list2not1
cd $1
find . >$list1
cd -
cd $2
find . >$list2
cd -
cat $list1 $list2 | sort >$listall
uniq -u $listall > $listdiff
cat $list1 $listdiff | sort | uniq -d >$list1not2
cat $list2 $listdiff | sort | uniq -d >$list2not1
# Now list1not2 contains the list of files in d1 but not in d2
# and similarly for list2not1
It doesn't actually create and populate the difference directories, but once you've got the two files that list the deltas, the rest is trivial. I leave that and the elimination of the tmp files as an exercise for the reader.

Post Reply