1
1
This commit is contained in:
2026-06-10 11:22:45 +02:00
parent 9f4f89763d
commit 317ea4db41
9 changed files with 178 additions and 18 deletions

56
compare_and_delete_larger.sh Executable file
View File

@@ -0,0 +1,56 @@
#!/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!"