Today I Learned - Rocky Kev

TIL this bash script

POSTED ON:

TAGS:

I share this not because I'm trying to master bash, or I think this is a magical piece of code (which I mean, it's pretty cool).

I share it because it's really easy to read, and a reminder that all langauges are kinda built the same.

What the script does is:

Find all files that have *.webp.
Starting with the first file...
Check if they have either a .jpg, .jpeg, .png, gif
If it does, then move mv (which will also rename it).

How to use it:

  1. Save this script somewhere, like replace-in-folder.sh
#!/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
  1. Then in your command line, you run it:
find . -type d -exec ./replace-in-folder.sh {} \;

Via:
https://web.archive.org/web/20210814220346/https://help.shortpixel.com/article/277-how-to-rename-my-webp-images-from-single-to-double-extension


Related TILs

Tagged:

TIL Evil code

These examples are unethical, and can even be criminal. You can get jailtime. You have been warned and I cannot be held liable for your dumb-assery.

TIL Play music in a bash script

On Macs, you have the afplay command. to play some sweet tunes.

TIL the fix everything script

A script that fixes everything for end-users