1

My iTunes library is on a NAS (WD MyCloud 4TB), and I have a number of TV series, organised by iTunes library in the usual:

'TV Shows' folder:

TV Show 1

|------------- Series 1
               |-----------01 Episode Name.m4v
               |-----------02 Episode Name.m4v
               |...
|------------- Series 2
               |-----------01 Episode Name.m4v
               |...
|...
TV Show 2
|------------- Series 1
               |-----------01 Episode Name.m4v
               |-----------02 Episode Name.m4v
               |...
|------------- Series 2
               |-----------01 Episode Name.m4v
               |...
|...

I have hard linked the whole TV folder to another folder, called Infuse. This folder will be read by the Infuse app on my Apple TV [this allows me to bypass having a computer with iTunes permanently switched on], so I need to rename all the hard-linked files as such:

TV Show 1
|------------- Series 1
               |-----------TV Show 1 - S0101 Episode Name.m4v
               |-----------TV Show 1 - S0102 Episode Name.m4v
               |...
|------------- Series 2
               |-----------TV Show 1 - S0201 Episode Name.m4v
               |...
|...
TV Show 2
|------------- Series 1
               |-----------TV Show 2 - S0101 Episode Name.m4v
               |-----------TV Show 2 - S0102 Episode Name.m4v
               |...
|------------- Series 2
               |-----------TV Show 2 - S0201 Episode Name.m4v
               |...
|...

I have so far found the solution (for example TV Show 1, Season 1 folder):

cd into each season folder for each show and run

for f in *; do mv $f "TV Show 1 E01S$f"; done

but this is really time consuming as I then need to

cd ../Season 2/
for f in *; do mv $f "TV Show 1 E02S$f"; done
cd ../Sesaon 3/

...

cd ../../TV Show 2/Season 1/
for f in *; do mv $f "TV Show 2 E01S$f"; done
...

and what I ideally want is to script pulling the name from the grandparent directory and the season number from the parent directory so I can write one short bash script and run it. Something like:

#!/bin/bash
a=[TV Show folder]
b=[Season folder]
c=[each episode]

for c in each a/b/
mv $c "$a - S0$bE$c"    (or)   rename $c "$a - S0$bE$c"
done

Can you help me implement it with a number of for loops or specific command?

1

Having done something very similar recently I already had a script.

for f in */*/*
do
  destdir=${f%/*}
  tvshow=${f%%/*}
  season=${destdir#*/}
  episode=${f##*/}
  # Get season number
  seasonnum=${season##* }

  dest=$(printf "%s/%s - S%02iE%s" "$destdir" "$tvshow" $seasonnum "$episode")
  echo "mv -- \"$f\" \"$dest\""
  # mv -- "$f" "$dest"
done
  • Thanks so much! This is amazing and completely works - such a fast response too!!! – JamieO Aug 9 '17 at 15:00
1

With zsh:

autoload zmv # best in ~/.zshrc
zmv -n '(*)/(Series (<->))/(<->)(*)' '$1/$2/$1 - S${(l:2::0:)3}E${(l:2::0:)4}$5'

(remove -n when happy).

  • <-> is <x-y> for numbers x to y but with neither x nor y provided, so any (decimal) number.
  • ${(l:2::0:)var}: left-pad of width 2 with 0s

Same with perl's rename:

rename -n 's{./(.*)/Series (\d+)/\K\d+}{
             sprintf "%s - S%02dE%02d", $1, $2, $&}se' ./*/*/*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.