Created
June 7, 2026 16:45
-
-
Save lopespm/2e0adf0ef9e2731b1bca63c38c50251c to your computer and use it in GitHub Desktop.
batch_convert_to_webp.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # Converts all .png/.jpg/.jpeg files under source/files/ to .webp | |
| # Skips conversion if the .webp already exists and is newer than the source file. | |
| set -euo pipefail | |
| SEARCH_DIR="$(dirname "$0")/some_sub_folder" | |
| find "$SEARCH_DIR" -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) | while IFS= read -r src; do | |
| basename=$(basename "$src") | |
| if [[ "$basename" == thumb* || "$basename" == pthumb* ]]; then | |
| echo "SKIP $src (thumbnail)" | |
| continue | |
| fi | |
| webp="${src%.*}.webp" | |
| if [[ -f "$webp" ]]; then | |
| src_mtime=$(stat -f "%m" "$src") | |
| webp_mtime=$(stat -f "%m" "$webp") | |
| if [[ "$src_mtime" == "$webp_mtime" ]]; then | |
| echo "SKIP $src (webp is up to date)" | |
| continue | |
| fi | |
| fi | |
| echo "CONVERT $src -> $webp" | |
| img2webp "$src" -o "$webp" | |
| touch -r "$src" "$webp" | |
| done | |
| echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment