Skip to content

Instantly share code, notes, and snippets.

@neomatrix369
Last active July 5, 2019 16:53
Show Gist options
  • Save neomatrix369/a945d9766ccedceadf37b97e467eeab7 to your computer and use it in GitHub Desktop.
Save neomatrix369/a945d9766ccedceadf37b97e467eeab7 to your computer and use it in GitHub Desktop.
createDiffs.sh bash script to show differences in files between two folders using the *nix command diff
#!/bin/bash
set -e
set -u
set -o pipefail
SOURCE_PATH=$1
DESTINATION_PATH=$2
sourceFiles=$(ls -l ${SOURCE_PATH}/ | wc -l)
echo "Number of files in source folder ${SOURCE_PATH}: ${sourceFiles}"
destinationFiles=$(ls -l ${DESTINATION_PATH}/ | wc -l)
echo "Number of files in destination folder ${DESTINATION_PATH}: ${destinationFiles}"
echo "Enlisting declarations that have changed"
rm which_declarations_differ.txt || true
diff -q --suppress-common-lines -y -d --strip-trailing-cr \
"${SOURCE_PATH}" "${DESTINATION_PATH}" > which_declarations_differ.txt || true
echo "Enlisting differences between the declarations (before and after)"
rm differences_between_changes.txt || true
touch differences_between_changes.txt
for filenameWithPath in ${SOURCE_PATH}/*; do
filenameOnly="$(basename ${filenameWithPath})"
#echo "Searching for ${filenameOnly} in which_declarations_differ.txt"
foundFile="$(grep ${filenameOnly} which_declarations_differ.txt || true)"
if [[ ! -z "${foundFile}" ]]; then
echo "Found ${filenameOnly} in which_declarations_differ.txt"
rm each_diff.txt || true
diff --suppress-common-lines -y -d --strip-trailing-cr "${SOURCE_PATH}/${filenameOnly}" "${DESTINATION_PATH}/${filenameOnly}" > each_diff.txt || true
if [[ -s each_diff.txt ]]; then
echo "" >> differences_between_changes.txt
echo "Comparing ${SOURCE_PATH}/${filenameOnly} and ${DESTINATION_PATH}/${filenameOnly}" >> differences_between_changes.txt
cat each_diff.txt >> differences_between_changes.txt
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> differences_between_changes.txt
echo "" >> differences_between_changes.txt
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment