Created
April 2, 2015 19:42
-
-
Save markusfisch/65e34aff854f7eb70a54 to your computer and use it in GitHub Desktop.
Isolate differences between two directories into a new directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Copy difference only | |
# | |
# @param 1 - input path to base version | |
# @param 2 - input path to extended version | |
# @param 3 - output path of diff-only version | |
cpdiff() | |
{ | |
(( $# < 3 )) && { | |
grep '^#[^!]' $0 | |
return 1 | |
} | |
[ -d "$3" ] && { | |
echo "$3 already exists" >&2 | |
return 1 | |
} | |
diff -rq $1 $2 | | |
# Files A and B differ | |
# Only in DIR: FILE | |
while read TYPE X DIR FILE X | |
do | |
case "$TYPE" in | |
Files*) | |
DST="$3/${FILE#*/}" | |
DST="${DST%/*}" | |
mkdir -p "$DST" && | |
cp "$FILE" "$DST/" | |
;; | |
Only*) | |
SRC=${DIR%:} | |
DST="$3/${SRC#*/}" | |
SRC="$SRC/$FILE" | |
mkdir -p "$DST" && | |
cp -r "$SRC" "$DST/" | |
;; | |
*) | |
echo "warning: cannot process line \"$TYPE...\"" >&2 | |
esac | |
done | |
} | |
if [ "$BASH_SOURCE" == "$0" ] | |
then | |
cpdiff "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment