2

I have two directories which are different versions of the same software package. I'd like to list all the files/directories that have changed between the two then copy those differences to a new directory.

I've been trying different scripts with md5sum and diff -Nurq but haven't been able to get the result I'm looking for.

Any recommendations?

Edit:

I originally tried taking md5sums, but that didn't seem to work especially if new files were missing.

Next I tried a loop like this:

for x in `diff -Nurq ./dir1/ ./dir2/ | awk -F" " '{print $4}'`
do 
  mkdir -vp ./dir_1_2_upgrade/$x; 
  cp $x ./dir_1_2_upgrade/$x
done

But that ended up making a bunch of directories and no files.

Rich
  • 12,068
  • 9
  • 62
  • 94
  • What did you try, and how did it fail? – Fred Foo May 17 '11 at 18:13
  • your method of copying files that have changes will never capture deletions, is that a bad thing? – Fred Foo May 17 '11 at 18:25
  • @larsmans, I think it's a good thing as I want to provide users with the option to add new stuff, but not clobber anything they might have built up before. Handling deletions sounds more complicated, right? – Rich May 17 '11 at 18:27

1 Answers1

1
source=SOME_DIR
dest=OTHER_DIR

# TODO: rewrite as awk script
delta=`diff -ur $source $dest`
deleted=`echo "$delta" | grep "^Only in ${source}:" | sed "s/^Only in ${source}: //"`
added=`echo "$delta"   | grep "^Only in ${dest}:"   | sed "s/^Only in ${dest}: //"`
changed=`echo "$delta" | grep '^\+\+\+' | awk '{print $2}'`

echo $deleted
echo $added
echo $changed

works if filenames contain no spaces. If you want this to be as general as possible, check out the source for a program such as git-diff.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836