diff --git a/push-newer-pics b/push-newer-pics index b7b8934..e0b23f1 100755 --- a/push-newer-pics +++ b/push-newer-pics @@ -1,9 +1,9 @@ #!/bin/bash # # push-newer-pics — Wrapper for sync-newer-images with default paths. -# Syncs ~/Pictures → ~/mnt/mini.nas/miniShare1/Pictures +# Copies new images from ~/Pictures → ~/mnt/mini.nas/miniShare1/Pictures # -# Usage: push-newer-pics [--help] +# Usage: push-newer-pics [--help] [-u|--update] SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd -P)" -exec "${SCRIPT_DIR}/sync-newer-images" "$HOME/mnt/mini.nas/miniShare1/Pictures" "$HOME/Pictures" +exec "${SCRIPT_DIR}/sync-newer-images" "$@" "$HOME/mnt/mini.nas/miniShare1/Pictures" "$HOME/Pictures" diff --git a/sync-newer-images b/sync-newer-images index 6d2fa35..c569388 100755 --- a/sync-newer-images +++ b/sync-newer-images @@ -1,35 +1,37 @@ #!/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. +# sync-newer-images — For each top-level image in SRC, copy it to DEST +# if it doesn't already exist anywhere under DEST. # Works on Linux and macOS (bash 3.2+ compatible). usage() { cat <<'EOF' Usage: sync-newer-images [options] -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 +For each top-level image in source, copy it into destination if no +file with the same name exists anywhere under destination. Options: + -u, --update If a matching file already exists under destination, + copy over it only if the content differs (uses cmp). -h, --help Show this help message and exit EOF exit 0 } -for arg in "$@"; do - case "$arg" in +update_mode=0 + +while [ $# -gt 0 ]; do + case "$1" in -h|--help) usage ;; + -u|--update) update_mode=1; shift ;; + *) break ;; esac done if [ $# -lt 2 ]; then echo "Error: destination and source are required" >&2 - echo "Usage: sync-newer-images " >&2 + echo "Usage: sync-newer-images [options] " >&2 exit 1 fi @@ -46,6 +48,9 @@ if [ ! -d "$SRC" ]; then exit 1 fi +# Build deduplicated, newline-separated list of all basenames under DEST. +basenames=$(find "$DEST" -type f 2>/dev/null | sed 's|.*/||' | sort -u) + copied=0 for srcfile in "$SRC"/*; do @@ -58,12 +63,17 @@ for srcfile in "$SRC"/*; do esac filename="${srcfile##*/}" - while IFS= read -r destfile; do - if [ -f "$destfile" ] && [ "$srcfile" -nt "$destfile" ]; then + + if ! printf '%s\n' "$basenames" | grep -qxF "$filename"; then + cp -v "$srcfile" "$DEST/" + copied=$((copied + 1)) + elif [ "$update_mode" -eq 1 ]; then + destfile=$(find "$DEST" -type f -name "$filename" 2>/dev/null | head -1) + if [ -n "$destfile" ] && ! cmp -s "$srcfile" "$destfile"; then cp -v "$srcfile" "$destfile" copied=$((copied + 1)) fi - done < <(find "$DEST" -type f -name "$filename" 2>/dev/null) + fi done -echo "Copied ${copied} newer image(s) from ${SRC} into ${DEST}" +echo "Copied ${copied} image(s) from ${SRC} into ${DEST}"