Skip to content

Instantly share code, notes, and snippets.

@jmar71n
Created May 23, 2010 10:54
Show Gist options
  • Save jmar71n/410839 to your computer and use it in GitHub Desktop.
Save jmar71n/410839 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Automatically sort TV Eppisodes/Series for XBMC Libary
# using Standard naming convention at http://wiki.xbmc.org/index.php?title=TV_Shows_(Video_Library)
# ie. /destination/directory/SERIES/SEASON NO./EPISODE
SOURCE=/source/directory
DEST=/destination/directory
############################################################################
cd $SOURCE
extract-function ()
{
# Find position of *.S01E01.*
POS=`expr "$file" : '.*[S,s][0-9][0-9][E,e][0-9][0-9]'`
# Extract series name from filename, and remove special characters
SERIES=${file:0:$((POS-7))}
SERIES=${SERIES//[.,-]/ }
# Extract Season no. from filename
if [ ${file:$((POS-5)):1} -eq 0 ]; then
SEASON=${file:$((POS-4)):1}
else
SEASON=${file:$((POS-5)):2}
fi
}
for file in *.avi; do
extract-function
echo "Move $file --> $DEST/$SERIES/Season $SEASON/"
done
echo -n "IS THIS CORRECT? (y/n) "
read YN
if [[ $YN = [y,Y] ]]; then
for file in *.avi; do
extract-function
# If destination does not exist, create it
if [ ! -d "$DEST/$SERIES/Season $SEASON" ] ; then
mkdir -p "$DEST/$SERIES/Season $SEASON/"
fi
# Move source file into new directory
mv "$SOURCE/$file" "$DEST/$SERIES/Season $SEASON/"
done
fi
# Ref.
# http://stackoverflow.com/questions/49403/how-do-you-parse-a-filename-in-bash
# http://learnlinux.tsf.org.za/courses/build/shell-scripting/ch03s04.html
# http://tldp.org/LDP/abs/html/string-manipulation.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment