[Bash] Move files/movies from a single directory to their own sub-directory!

After spending a couple hours moving all my movies from a single massive folder to their own sub-directories, I decided “SCREW THIS” and decided to create a bash script.

I got my solution from trawling through various Stack Overflow posts on Bash posts. Apologies, I should have noted the references but didn’t think this would be so easy! I also don’t have enough rep to vote up either (rather annoying!).

As you might have noticed from my other scripts, I like to log things, so everything gets logged :D

Brief
Move all movies and their associated files into their own sub-directory, taking into account multi-part files (e.g. “Movie CD01/CD02”).

Pre-Requisites
Sounds a bit naff but this script relies on one thing: The movie name is consistent across each movie, i.e. the movies are already neatly named. If you use SABNzbd/Couchpotato, this shouldn’t be a problem. To give you an example, all my movies are named as follows: “Winnie The Pooh (2011) (720p).mkv” or in the case of multi-parts “Annie (1982) CD1.avi” and “Annie (1982) CD2.avi”.

How To Use
Pretty simple, start up a shell (be it via PuTTY or KiTTYTerm as I now use), “cd” to the Movie folder and paste this sucker in.

Script Rundown

  • Basically it goes through the current directory, finds every AVI, MKV, WMV, MP4, TS and MPG file.
  • For each file it finds, it …
    • Strips the multi-part identifier. This can be either CD1 or CD02 or CD99, and combination of single or double digit from 0 to 99;
    • Checks to see if the folder exists, if it doesn’t create it using the movie name excluding the multi-part number (and logs it);
    • Moves (verbosely) all files that start with the movie title into the new directory (and logs it!).

and that’s it!

Next I have to re-scan using XBMC then clean my library to remove the old references and I’m all done! YIPPIE KAI YAY!

If you like this, then go donate to some kitten patting charity.