Files
schmeeve-toolz/sync-newer-images

70 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
#
# sync-newer-images — For each top-level image in SRC, find any matching
# filename recursively under DEST and copy over it if SRC is newer.
# Works on Linux and macOS (bash 3.2+ compatible).
usage() {
cat <<'EOF'
Usage: sync-newer-images [options] <destination> <source>
For each top-level image in source, find any matching filename recursively
under destination and copy over it if the source file is newer.
Arguments:
destination Path to copy TO
source Path to copy FROM
Options:
-h, --help Show this help message and exit
EOF
exit 0
}
for arg in "$@"; do
case "$arg" in
-h|--help) usage ;;
esac
done
if [ $# -lt 2 ]; then
echo "Error: destination and source are required" >&2
echo "Usage: sync-newer-images <destination> <source>" >&2
exit 1
fi
DEST="$1"
SRC="$2"
if [ ! -d "$DEST" ]; then
echo "Error: destination '$DEST' is not a directory" >&2
exit 1
fi
if [ ! -d "$SRC" ]; then
echo "Error: source '$SRC' is not a directory" >&2
exit 1
fi
copied=0
for srcfile in "$SRC"/*; do
[ -f "$srcfile" ] || continue
ext="${srcfile##*.}"
case "$(echo "$ext" | tr '[:upper:]' '[:lower:]')" in
jpg|jpeg|png|gif|heic|webp|bmp|tiff|tif) ;;
*) continue ;;
esac
filename="${srcfile##*/}"
while IFS= read -r destfile; do
if [ -f "$destfile" ] && [ "$srcfile" -nt "$destfile" ]; then
cp -v "$srcfile" "$destfile"
copied=$((copied + 1))
fi
done < <(find "$DEST" -type f -name "$filename" 2>/dev/null)
done
echo "Copied ${copied} newer image(s) from ${SRC} into ${DEST}"