From fff8e346ad49ba68239902e3f608e6f6c1299928 Mon Sep 17 00:00:00 2001 From: jim kolpack Date: Fri, 1 May 2026 21:58:31 -0400 Subject: [PATCH] Make scripts generic and fix several bugs - Add optional directory argument to images_resize_recursive.sh, zip_images_resize_recursive.sh, and new rar2zip_recursive.sh - Remove hardcoded-path wrappers rar2zip_images.sh and rar2zip_incomplete.sh - Fix zip_images_resize.sh: move SCRIPT_DIR to top, change exit 1 to continue on bad output file so remaining files still process - Use MAX_PIXELS variable in images_resize.sh resize argument - Fix zip_images_recursive.sh: replace case-sensitive 7z globs with find -iregex piped via xargs --- images_resize.sh | 2 +- images_resize_recursive.sh | 10 +++++++++- rar2zip_images.sh | 3 --- rar2zip_incomplete.sh | 3 --- rar2zip_recursive.sh | 21 +++++++++++++++++++++ zip_images_recursive.sh | 8 ++++---- zip_images_resize.sh | 5 +++-- zip_images_resize_recursive.sh | 17 +++++++++++++++-- 8 files changed, 53 insertions(+), 16 deletions(-) delete mode 100755 rar2zip_images.sh delete mode 100755 rar2zip_incomplete.sh create mode 100755 rar2zip_recursive.sh diff --git a/images_resize.sh b/images_resize.sh index 4fd000b..da08804 100755 --- a/images_resize.sh +++ b/images_resize.sh @@ -21,7 +21,7 @@ do # Get file size before resizing SIZE_BEFORE=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null) - ~/Applications/magick "$f" -resize 8000000@@\> -sampling-factor 4:2:0 -strip -quality 85% "$f" + ~/Applications/magick "$f" -resize "${MAX_PIXELS}@@\>" -sampling-factor 4:2:0 -strip -quality 85% "$f" # Get file size after resizing SIZE_AFTER=$(stat -f%z "$f" 2>/dev/null || stat -c%s "$f" 2>/dev/null) diff --git a/images_resize_recursive.sh b/images_resize_recursive.sh index 0b8a0b9..90dedce 100755 --- a/images_resize_recursive.sh +++ b/images_resize_recursive.sh @@ -1,8 +1,16 @@ #!/bin/bash +SEARCH_DIR="${1:-.}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' f +if [ ! -d "$SEARCH_DIR" ]; then + echo "Error: '$SEARCH_DIR' is not a directory" + exit 1 +fi + +echo "Resizing images recursively in $SEARCH_DIR" + +find "$SEARCH_DIR" -mindepth 1 -type d -print0 | while IFS= read -r -d '' f do if pushd "$f" > /dev/null; then "$SCRIPT_DIR/images_resize.sh" diff --git a/rar2zip_images.sh b/rar2zip_images.sh deleted file mode 100755 index 5116dfa..0000000 --- a/rar2zip_images.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -find /media/rewt/media/images/ -name '*.rar' -exec "$SCRIPT_DIR/rar2zip.sh" '{}' \; diff --git a/rar2zip_incomplete.sh b/rar2zip_incomplete.sh deleted file mode 100755 index d5c48db..0000000 --- a/rar2zip_incomplete.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -find /media/rewt/media/incomplete/ -name '*.rar' -exec "$SCRIPT_DIR/rar2zip.sh" '{}' \; diff --git a/rar2zip_recursive.sh b/rar2zip_recursive.sh new file mode 100755 index 0000000..afeafef --- /dev/null +++ b/rar2zip_recursive.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# Recursively find all RAR files in a directory and convert them to ZIP. +# +# Usage: rar2zip_recursive.sh [directory] +# directory Directory to search (default: current directory) + +SEARCH_DIR="${1:-.}" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +if [ ! -d "$SEARCH_DIR" ]; then + echo "Error: '$SEARCH_DIR' is not a directory" + exit 1 +fi + +echo "Converting RARs to ZIPs in $SEARCH_DIR" + +find "$SEARCH_DIR" -name '*.rar' -print0 | while IFS= read -r -d '' f +do + "$SCRIPT_DIR/rar2zip.sh" "$f" +done diff --git a/zip_images_recursive.sh b/zip_images_recursive.sh index 960861e..5f1eaa2 100755 --- a/zip_images_recursive.sh +++ b/zip_images_recursive.sh @@ -20,11 +20,11 @@ if [ "$#" -ne 0 ]; then directoryTree="${1} - ${directoryTree}" fi -jpgs=$(find . -maxdepth 1 -regextype posix-egrep -regex ".*\.(jpg|JPG|JPEG|jpeg)$") +mapfile -d '' jpgs < <(find . -maxdepth 1 -regextype posix-egrep -iregex ".*\.(jpg|jpeg)$" -print0) -if [ -n "$jpgs" ]; then - echo "Zipping JPGs in ${directoryTree}" - 7z a -tzip -mx=0 "$directoryTree.zip" "*.jpg" -i!*.jpeg -i!*.JPG -i!*.JPEG +if [ ${#jpgs[@]} -gt 0 ]; then + echo "Zipping JPGs in ${directoryTree}" + printf '%s\0' "${jpgs[@]}" | xargs -0 7z a -tzip -mx=0 "$directoryTree.zip" fi diff --git a/zip_images_resize.sh b/zip_images_resize.sh index 5bd829a..5b5d755 100755 --- a/zip_images_resize.sh +++ b/zip_images_resize.sh @@ -10,6 +10,8 @@ if [ $# -eq 0 ]; then exit 1 fi +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + echo "Resizing large JPGs in ZIPs" # Use RAM disk for temporary files. @@ -64,7 +66,6 @@ for INFILE in "$@"; do cleanup_temp continue fi - SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" "$SCRIPT_DIR/images_resize.sh" popd > /dev/null @@ -88,7 +89,7 @@ for INFILE in "$@"; do if [ ! -s "$OUTFILE" ]; then echo "Error: Output file is empty or does not exist. Keeping original file." cleanup_temp - exit 1 + continue fi rm "$INFILE" diff --git a/zip_images_resize_recursive.sh b/zip_images_resize_recursive.sh index 4d92651..51f7d1e 100755 --- a/zip_images_resize_recursive.sh +++ b/zip_images_resize_recursive.sh @@ -1,4 +1,17 @@ #!/bin/bash -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -find . -iname '*.zip' -exec "$SCRIPT_DIR/zip_images_resize.sh" '{}' \; + +SEARCH_DIR="${1:-.}" +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +if [ ! -d "$SEARCH_DIR" ]; then + echo "Error: '$SEARCH_DIR' is not a directory" + exit 1 +fi + +echo "Resizing images in ZIPs recursively in $SEARCH_DIR" + +find "$SEARCH_DIR" -iname '*.zip' -print0 | while IFS= read -r -d '' f +do + "$SCRIPT_DIR/zip_images_resize.sh" "$f" +done