Script to determine directory 'diff'
-
14-11-2019 - |
Question
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 md5sum
s, 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.
Solution
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
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow