Announcement

Collapse
No announcement yet.

OK bash geniuses... Mass file renaming of different types and subfolders! Help!

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    OK bash geniuses... Mass file renaming of different types and subfolders! Help!

    I'm re-aligning my media on my server due to a change in content delivery software. The new program wants some stuff different - the key piece being the title of a movie should be followed by a space then the year in which it was released parenthetically. I.e., what previously was "13 Assassins" should now be "13 Assassins (2011)"

    These movies are stored in individual subfolders to allow use of poster and fan art .jpg's as icon and background. I was able to extract the year for each movie from the movie info file (no longer being used by the new software) and put it into a file names year (containing the four number year and nothing else) within the folder and use that file to rename all the subfolders like this:

    OIFS="$IFS"; IFS=$'\n'; for subdir in *; do mv $subdir $subdir" (`cat $subdir/year`)" ; done; IFS="$OIFS";

    The $IFS manipulation was done to allow for names with spaces in them. Not all the movies had info files but that just created an empty year file so the result would look like "13 Assassins ()" which is fine - I can manually fix those few out of the 400+ folders/movies.

    Now on to renaming the movies. Here's the catch: They are of different file types: mp4, m4v, avi, mkv, and maybe a couple others. I want to do something similar to the above for the files. I would accept a solution that requires it to be run once for each type, but a really neat and tidy single run would be very cool. Also, there are other files in most of the subfolders like poster.jpg, fanart.jpg, and subtitles of various formats so all other types - with or without extensions - must be ignored.

    Here's an example folder and it's contents:

    /media/videos/Movies
    /13 Assassins (2011)
    13 Assassins.mp4
    poster.jpg
    fanart.jpg
    year

    Some will have an additional nested folder named /SRT with subtitles and others have additional fun movie stuff in them.

    Any ideas?

    Please Read Me

    #2
    Maybe not elegant but this should work:
    Code:
    for movieyear in *; do movie=$(echo $movieyear | cut -d'(' -f1 - | sed 's/[ ]$//'); cd "$movieyear"; rename -v s/"$movie"/"$movieyear"/ "$movie".*; cd ..; done
    Let's test it first, with a -n option in the rename command first:
    Code:
    steve@t520:~/junk$ [B]for movieyear in *; do movie=$(echo $movieyear | cut -d'(' -f1 - | sed 's/[ ]$//'); cd "$movieyear"; rename -v -n s/"$movie"/"$movieyear"/ "$movie".*; cd ..; done[/B]
    rename(movie name 1.mp4, movie name 1 (year1).mp4)
    rename(movie name 2.avi, movie name 2 (year2).avi)
    Looks good, so let's run it on a sample set of two: each "movie (year)" folder contains a "movie.ext" file.
    Code:
    steve@t520:~/junk$ [B]ll -R[/B]
    .:
    total 0
    drwxrwxr-x 1 steve steve   80 Dec 12 16:02 ./
    drwxr-xr-x 1 steve steve 1268 Dec 12 17:11 ../
    drwxrwxr-x 1 steve steve   52 Dec 12 17:12 movie name 1 (year1)/
    drwxrwxr-x 1 steve steve   52 Dec 12 17:12 movie name 2 (year2)/
    
    ./movie name 1 (year1):
    total 4
    drwxrwxr-x 1 steve steve 52 Dec 12 17:12 ./
    drwxrwxr-x 1 steve steve 80 Dec 12 16:02 ../
    -rw-rw-r-- 1 steve steve 13 Dec 12 15:45 movie name 1.mp4
    -rw-rw-r-- 1 steve steve  0 Dec 12 17:12 poster.jpg
    
    ./movie name 2 (year2):
    total 4
    drwxrwxr-x 1 steve steve 52 Dec 12 17:12 ./
    drwxrwxr-x 1 steve steve 80 Dec 12 16:02 ../
    -rw-rw-r-- 1 steve steve 12 Dec 12 16:38 movie name 2.avi
    -rw-rw-r-- 1 steve steve  0 Dec 12 17:12 poster.jpg
    
    
    steve@t520:~/junk$ [B]for movieyear in *; do movie=$(echo $movieyear | cut -d'(' -f1 - | sed 's/[ ]$//'); cd "$movieyear"; rename -v s/"$movie"/"$movieyear"/ "$movie".*; cd ..; done[/B]
    movie name 1.mp4 renamed as movie name 1 (year1).mp4
    movie name 2.avi renamed as movie name 2 (year2).avi
    
    
    steve@t520:~/junk$ [B]ll -R[/B]
    .:
    total 0
    drwxrwxr-x 1 steve steve   80 Dec 12 16:02 ./
    drwxr-xr-x 1 steve steve 1268 Dec 12 17:11 ../
    drwxrwxr-x 1 steve steve   68 Dec 12 17:13 movie name 1 (year1)/
    drwxrwxr-x 1 steve steve   68 Dec 12 17:13 movie name 2 (year2)/
    
    ./movie name 1 (year1):
    total 4
    drwxrwxr-x 1 steve steve 68 Dec 12 17:13 ./
    drwxrwxr-x 1 steve steve 80 Dec 12 16:02 ../
    -rw-rw-r-- 1 steve steve 13 Dec 12 15:45 movie name 1 (year1).mp4
    -rw-rw-r-- 1 steve steve  0 Dec 12 17:12 poster.jpg
    
    ./movie name 2 (year2):
    total 4
    drwxrwxr-x 1 steve steve 68 Dec 12 17:13 ./
    drwxrwxr-x 1 steve steve 80 Dec 12 16:02 ../
    -rw-rw-r-- 1 steve steve 12 Dec 12 16:38 movie name 2 (year2).avi
    -rw-rw-r-- 1 steve steve  0 Dec 12 17:12 poster.jpg
    In your case, you'll want to run this command inside /media/videos/Movies.

    Comment


      #3
      Thanks a lot Steve, I kinda figured you'd come up with an answer! I was going to work on it this morning as my PC brain was fried around 6pm last night when I posted this thread. I needed an evening off.

      I should really put this and a couple other commands I used on this project into a script so I can run it whenever I add some new stuff.

      Please Read Me

      Comment


        #4
        It was actually a fun challenge for a Friday afternoon!

        I'd imagine that judicious use of awk would probably make for something more efficient, but that's a tool I haven't yet spent much time learning. And rename is much better than mv for the purpose here, since it supports regex substitutions and preserves file extensions.

        Comment

        Working...
        X