Skip to content

Instantly share code, notes, and snippets.

@masterflitzer
Last active April 21, 2025 18:35
Show Gist options
  • Save masterflitzer/30c9002dfe491bd1952b9ce413072bc8 to your computer and use it in GitHub Desktop.
Save masterflitzer/30c9002dfe491bd1952b9ce413072bc8 to your computer and use it in GitHub Desktop.
consistent naming for tv shows in multimedia collection
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
shopt -s inherit_errexit
confirm="${1:-no}"
series="${PWD##*/}"
series="$(tr '[:upper:]' '[:lower:]' <<< "${series}")"
series="${series%-*}"
readarray -t seasons < <(find . -mindepth 1 -maxdepth 1 -type d | sort)
for season in "${seasons[@]}"
do
season="${season##*/}"
readarray -t episodes < <(find "${season}" -mindepth 1 -maxdepth 1 -type f | sort)
for episode in "${episodes[@]}"
do
episode="${episode##*/}"
s="$(grep -Eio 's[0-9]{2}' <<< "${episode}")"
s="$(tr '[:upper:]' '[:lower:]' <<< "${s}")"
s="${s#s}"
e="$(grep -Eio 'e[0-9]{2}' <<< "${episode}")"
e="$(tr '[:upper:]' '[:lower:]' <<< "${e}")"
e="${e#e}"
ext="${episode##*.}"
ext="$(tr '[:upper:]' '[:lower:]' <<< "${ext}")"
if test -n "${series}" && test -n "${s}" && test -n "${e}" && test -n "${ext}"
then
fileOld="${season}/${episode}"
fileNew="${season}/${series}-s${s}e${e}.${ext}"
if test "${confirm}" = "yes" && test "${fileOld}" != "${fileNew}"
then
mv "${fileOld}" "${fileNew}"
else
echo mv "${fileOld}" "${fileNew}"
fi
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment