#!/bin/bash # Script to compare .png/.jpg with their .webp versions and delete the larger one # Usage: ./compare_and_delete_larger.sh [directory] # Default: current directory set -euo pipefail TARGET_DIR="${1:-.}" echo "Scanning for image files in: $TARGET_DIR" # Function to process a single image file process_file() { local original="$1" local webp_file="${original}.webp" # Check if webp version exists if [[ ! -f "$webp_file" ]]; then echo "[SKIP] No webp version for: $original" return 0 fi # Get file sizes local original_size local webp_size original_size=$(stat -c%s "$original" 2>/dev/null || echo "0") webp_size=$(stat -c%s "$webp_file" 2>/dev/null || echo "0") echo "[COMPARE] $original ($(numfmt --to=iec $original_size)) vs $webp_file ($(numfmt --to=iec $webp_size))" # Compare sizes if [[ "$original_size" -gt "$webp_size" ]]; then echo "[DELETE] $original is larger, deleting..." rm -v "$original" elif [[ "$original_size" -lt "$webp_size" ]]; then echo "[DELETE] $webp_file is larger, deleting..." rm -v "$webp_file" else echo "[SKIP] Both files have the same size" fi } # Export the function for use with find -exec Export_func() { export -f process_file } Export_func # Find all .png and .jpg files (case insensitive) and process them find "$TARGET_DIR" -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | while IFS= read -r -d '' file; do process_file "$file" done echo "" echo "Done!"