Skip to content

Instantly share code, notes, and snippets.

@jgavinray
Created June 20, 2019 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgavinray/59b320c4643ddc1f099d4420503fc9f4 to your computer and use it in GitHub Desktop.
Save jgavinray/59b320c4643ddc1f099d4420503fc9f4 to your computer and use it in GitHub Desktop.
Script to rename files to be plex compatible
#!/bin/bash
# Define show name
SHOW_NAME="Battlestar Galactica"
# Loop through all m4v files
for f in *.m4v
do
# Get episode number, fifth column - srip trailing underscore
EPISODE=$(echo $f | awk '{print $5;}' | sed 's/_//g')
# Get episode number length
EPISODE_LEN=${#EPISODE}
if [ "$EPISODE_LEN" == 1 ]; then
echo "Episode length isn't long enough, prepending a 0"
EPISODE="0${EPISODE}"
fi
# Get season from this column, strip off trailing comma
SEASON=$(echo $f | awk '{print $3;}' | sed 's/,//g')
# Preped 0 infront of season integer
SEASON="0$SEASON"
# Check is season directory exists, if not create it.
SEASON_DIR="Season ${SEASON}"
# Set the Final Dir here so we can set it later
CURRENT_DIR=`pwd`
MOVE_DIR="$CURRENT_DIR/$SEASON_DIR"
if [ -d "$SEASON_DIR" ]; then
echo "$SEASON_DIR directory already exists - moving on."
# If it exists break out of if check
:
else
mkdir "$MOVE_DIR"
fi
# Compose the title and file type - get all columns after 6 and put them together
TITLE=$(echo $f | awk '{ s = ""; for (i = 6; i <= NF; i++) s = s $i " "; print s }' )
# Finally! Copy the files!
echo "Copying: '${f} to '${MOVE_DIR}/${SHOW_NAME} - s${SEASON}e${EPISODE} - ${TITLE}'"
cp "${f}" "${MOVE_DIR}/${SHOW_NAME} - s${SEASON}e${EPISODE} - ${TITLE}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment