Find different files on the server (difference between two folders)
Sometimes it can be very practical to find the files with differences between two directories, e.g. for store systems and other web applications. This allows the contents of two folders to be compared in order to specifically find the changed files – e.g. between a store system used in production and a fresh (empty) installation of the same version.
If you do not understand the following examples, you should not execute the shell script yourself, as this may entail security risks.
Example shell script which only packs changed files into a zip archive:
#!/bin/bash
SOURCE="/var/www/vhosts/example.com/httpdocs"
REFERENCE="/var/www/vhosts/test.example.com/reference"
ZIPFILE="/var/www/vhosts/example.com/difference.zip"
TMPDIFFDIR=$(mktemp -d)
cd "$SOURCE" || exit 1
find . -type f | while read -r file; do
srcfile="$SOURCE/$file"
reffile="$REFERENCE/$file"
# Only compare files that exist in REFERENCE
if [ -f "$reffile" ]; then
if ! diff -q -u <(sed 's/[[:space:]]*$//' "$srcfile" | tr -d '\r') <(sed 's/[[:space:]]*$//' "$reffile" | tr -d '\r') >/dev/null 2>&1; then
mkdir -p "$TMPDIFFDIR/$(dirname "$file")"
cp --parents "$srcfile" "$TMPDIFFDIR"
fi
fi
done
cd "$TMPDIFFDIR" || exit 1
zip -r "$ZIPFILE" ./*
rm -rf "$TMPDIFFDIR"
echo "Done! Differences saved in $ZIPFILE"
and a variant which also considers new files not present in the reference in addition to the changed files:
#!/bin/bash
SOURCE="/var/www/vhosts/example.com/httpdocs"
REFERENCE="/var/www/vhosts/test.example.com/reference"
ZIPFILE="/var/www/vhosts/example.com/difference.zip"
TMPDIFFDIR=$(mktemp -d)
cd "$SOURCE" || exit 1
find . -type f | while read -r file; do
srcfile="$SOURCE/$file"
reffile="$REFERENCE/$file"
if [ ! -f "$reffile" ]; then
mkdir -p "$TMPDIFFDIR/$(dirname "$file")"
cp --parents "$file" "$TMPDIFFDIR"
else
if ! diff -q -u <(sed 's/[[:space:]]*$//' "$srcfile" | tr -d '\r') <(sed 's/[[:space:]]*$//' "$reffile" | tr -d '\r') >/dev/null 2>&1; then
mkdir -p "$TMPDIFFDIR/$(dirname "$file")"
cp --parents "$file" "$TMPDIFFDIR"
fi
fi
done
cd "$TMPDIFFDIR" || exit 1
zip -r "$ZIPFILE" ./
rm -rf "$TMPDIFFDIR"
echo "Done! Differences saved in $ZIPFILE"