View Categories

How to rename my WebP images from single to double extension

If you generated the WebP images with a single extension (image.webp) and want to use a plugin (like LiteSpeed Cache) that requires WebP files with a double extension (image.jpg.webp), you just need to have access to a shell console with Bash. If you are not sure, please ask your hosting provider or a developer you trust.

Simply run this script for all images in a folder (pass the folder as a 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 will need to do this in each folder or include it in a shell script code that traverses the directory structure. To do this, you could use a command like the following (for example, if the script above is called “replace-in-folder.sh”):

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