How to rename my WebP images from single to double extension

If you generated the WebP images with single extension ( image.webp) and you want to start using a cache plugin (like LiteSpeed) that needs double extension like image.jpg.webp and you have access to a shell console with Bash, you can run a script like this one to do it for all the images in a folder (pass the folder as parameter).

#!/bin/bash
cd $1
for file in *.webp
do
  if test -f "${file%.webp}.jpg"
  then
    echo "${file%.webp}.jpg" EXISTS. Moving "$file" to "${file%.webp}.jpg.webp"
    mv "$file" "${file%.webp}.jpg.webp"
  fi

  if test -f "${file%.webp}.jpeg"
  then
    echo "${file%.webp}.jpeg" EXISTS. Moving "$file" to "${file%.webp}.jpeg.webp"
    mv "$file" "${file%.webp}.jpeg.webp"
  fi

  if test -f "${file%.webp}.png"
  then
    echo "${file%.webp}.png" EXISTS. Moving "$file" to "${file%.webp}.png.webp"
    mv "$file" "${file%.webp}.png.webp"
  fi

  if test -f "${file%.webp}.gif"
  then
    echo "${file%.webp}.gif" EXISTS. Moving "$file" to "${file%.webp}.gif.webp"
    mv "$file" "${file%.webp}.gif.webp"
  fi
done

You need to do this in each folder or wrap it into shell script code that walks the directory structure. For this you could use a command like this (if the script above is called replace-in-folder.sh):

find . -type d -exec ./replace-in-folder.sh {} \;

Still need help? Contact Us Contact Us