Create a gist now

Instantly share code, notes, and snippets.

@ctrezevant /
Last active Aug 29, 2015

What would you like to do?
# Get num of episodes in the current directory by
# Searching for files that contain 'Episode' in the name.
EP=`ls | grep -i 'Episode' | wc -l`
# Get current season from the name of the directory we're currently in
# This assumes that episodes are in directories by episode, like so:
# /path/to/TV Show/Season 1/
SEASON=`pwd | grep -o '[0-9]*'`
# Initialize the variable we'l be using for the while loop.
#We need to create a directory to hold the sorted episodes so we don't accidentally mess up the counts in the while loop.
mkdir ./sorted
while [ $I -le $EP ]
# Get source episode filename from increment number
OLDEP=`ls | sort -n -t ' ' -k 2 | grep -m 1 $I`
# Extract episode name
EPNAME=`ls | sort -n -t ' ' -k 2 | grep -m 1 $I | cut -d '-' -f2 | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12}' | cut -d '.' -f1`
# Construct the new name of the episode.
NEWEP='S0'${SEASON}'E'${I}' - '${EPNAME}'.mkv'
echo '--'
echo "Increment: "$I
echo "Old filename: "$OLDEP
echo "Found full episode name for episode "${I}": " $EPNAME
echo "Constructed target Filename: "$NEWEP
# Move the episide to the sorted directory, thus changing the name.
sudo mv -v "$OLDEP" ./sorted/"$NEWEP"
echo '--'
# Move all sorted episodes to current directory, and clean up.
mv ./sorted/* ./
rm -r sorted/
echo 'All done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment