fix push pics script
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#
|
#
|
||||||
# push-newer-pics — Wrapper for sync-newer-images with default paths.
|
# 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)"
|
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"
|
||||||
|
|||||||
@@ -1,35 +1,37 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
#
|
#
|
||||||
# sync-newer-images — For each top-level image in SRC, find any matching
|
# sync-newer-images — For each top-level image in SRC, copy it to DEST
|
||||||
# filename recursively under DEST and copy over it if SRC is newer.
|
# if it doesn't already exist anywhere under DEST.
|
||||||
# Works on Linux and macOS (bash 3.2+ compatible).
|
# Works on Linux and macOS (bash 3.2+ compatible).
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<'EOF'
|
cat <<'EOF'
|
||||||
Usage: sync-newer-images [options] <destination> <source>
|
Usage: sync-newer-images [options] <destination> <source>
|
||||||
|
|
||||||
For each top-level image in source, find any matching filename recursively
|
For each top-level image in source, copy it into destination if no
|
||||||
under destination and copy over it if the source file is newer.
|
file with the same name exists anywhere under destination.
|
||||||
|
|
||||||
Arguments:
|
|
||||||
destination Path to copy TO
|
|
||||||
source Path to copy FROM
|
|
||||||
|
|
||||||
Options:
|
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
|
-h, --help Show this help message and exit
|
||||||
EOF
|
EOF
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in "$@"; do
|
update_mode=0
|
||||||
case "$arg" in
|
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
-h|--help) usage ;;
|
-h|--help) usage ;;
|
||||||
|
-u|--update) update_mode=1; shift ;;
|
||||||
|
*) break ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ $# -lt 2 ]; then
|
if [ $# -lt 2 ]; then
|
||||||
echo "Error: destination and source are required" >&2
|
echo "Error: destination and source are required" >&2
|
||||||
echo "Usage: sync-newer-images <destination> <source>" >&2
|
echo "Usage: sync-newer-images [options] <destination> <source>" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -46,6 +48,9 @@ if [ ! -d "$SRC" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
copied=0
|
||||||
|
|
||||||
for srcfile in "$SRC"/*; do
|
for srcfile in "$SRC"/*; do
|
||||||
@@ -58,12 +63,17 @@ for srcfile in "$SRC"/*; do
|
|||||||
esac
|
esac
|
||||||
|
|
||||||
filename="${srcfile##*/}"
|
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"
|
cp -v "$srcfile" "$destfile"
|
||||||
copied=$((copied + 1))
|
copied=$((copied + 1))
|
||||||
fi
|
fi
|
||||||
done < <(find "$DEST" -type f -name "$filename" 2>/dev/null)
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Copied ${copied} newer image(s) from ${SRC} into ${DEST}"
|
echo "Copied ${copied} image(s) from ${SRC} into ${DEST}"
|
||||||
|
|||||||
Reference in New Issue
Block a user