Skip to content

Instantly share code, notes, and snippets.

@markusfisch
Created April 2, 2015 19:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markusfisch/65e34aff854f7eb70a54 to your computer and use it in GitHub Desktop.
Save markusfisch/65e34aff854f7eb70a54 to your computer and use it in GitHub Desktop.
Isolate differences between two directories into a new directory
#!/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