Im trying to move files into subfolders based on a regular expression. For example im trying to move tv shows into the correct show and season folder. All files follow the pattern "Show Name.S00E00.episode title.avi".

I've looked at mmv and rename but can't seem to find any helpful examples.

If someone could point me in the right direction I would be very grateful.

Edit: I forgot to mention the folder structure is

./
   Unsorted video files 
   Show Name/
      Season 1/
           Video Files Sorted
      Season 2/
link|improve this question
feedback

1 Answer

up vote 2 down vote accepted

For the bash shell:

for file in *.avi; do
    # use perl to transform the file name; could use 'sed -r' too
    new_path=$(perl -pe 's|^(.+?)\.S0*(\d+)E0*(\d+)\.(.+)\.(\w+)$|$1/Season $2/Episode $3 - $4.$5|' <<< "$file")
    # create directories if needed
    mkdir -p "${new_path%/*}"
    # move the file
    mv -vn "$file" "$new_path"
done

In this regexp example (s|...|...|):

  • $1 is the show name (everything up to ".S<num>E<num>");
  • $2 and $3 are season/episode numbers;
  • $4 is the episode title (everything up to the final ".");
  • $5 is the file extension.

If you want to keep leading zeroes in season/episode numbers, replace S0* and E0* with just S and E. If you want to process other file types, adjust *.avi in the first line.


Debian and Ubuntu come with a Perl-based prename script which could be used for this, but it doesn't create the new directories automatically. It's easy to modify it to do so; then you can just use prename 'the above regexp' *.avi.)

link|improve this answer
Can you explain why (.+?) for season name, why not just (.+) – bbaja42 Jun 11 '11 at 11:02
That worked perfectly. Thank you! :D – AverageMarcus Jun 11 '11 at 11:31
feedback

Your Answer

 
or
required, but never shown

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