I have been trying to use the below script to link all of the tv shows in my itunes eg:
to a new directory with the format of ./TVShows/Show.Name/Season.nn/Show.Name.SnnEnn.Episode.Name.m4v e.g:
#! /bin/bash
srcroot="./iTunes/iTunes Media/TV Shows"
destroot="./TVShows"
for f in "$srcroot"/*/*/*.{m4v,mp4,avi}
do
[[ -f $f ]] || continue
IFS=/ read -a vid <<< "${f// /.}"
vid=( "${vid[@]: -3}" )
show=${vid[0]}
season=${vid[1]}
episode=${vid[2]}
[[ $show =~ \([0-9]{4}\)$ ]] && show=${show: 0:${#show}-6}${show: ${#show}-5:4}
sn=${season#*.}
IFS=. read ep eptitle <<< "${episode%.*}"
ext=${episode##*.}
printf -v hldir -- '%s/%s/%s' "$destroot" "$show" "$season"
printf -v hlname -- '%s.s%02de%02d.%s.%s' "$show" "$sn" "$ep" "$eptitle" "$ext"
echo "from: '$f'"
echo "to: '$hldir/$hlname'"
mkdir -p "$hldir"
ln "$f" "$hldir/$hlname"
echo
done
This script seems to mostly do what I want, except whenever it encounters episode numbers 08 or 09 e.g:
gets linked to:
Can anyone tell me why this is happening, and how I can fix it?
printf %02d 08 09
fails with
bash: printf: 08: invalid octal number
bash: printf: 09: invalid octal number
That's because numbers starting with 0 are interpreted as octal numbers, and octal numbers can only contain characters 0-7. Remove the leading zeros. In bash, you can use
x=008
printf %02d $((10#$x))
which interprets the number as decimal.
You can also use $(printf %s "$x" | sed 's/^00*//')