Script to order my video library :-)

Dear friends,
I have a hundred or so videos I keep in a directory, but as the collection has grown anarchically in the past years I have at least 3 different situations:

a) one directory for each video and within it the video and any other accompaining files (mostly text files with my notes and reviews). This is the desired end state for all of them

b) a single movie file sitting in the top directory (extensions can be flc, mkv, avi,mpg etc.). These I would like to move into a directory with the same name as the file

c) single non-movie files. these I'd ignore for now.

Thanks for any help.

If these files have been renamed by hand and such, organizing these by computer, matching dissimilar names, may be extremely difficult.

Am I correct in interpreting your requirements to be:

  1. You are sitting in a directory that contains both regular files and directories.
  2. You do not want to do anything with any files in directories under the directory in which you're sitting.
  3. You want to move regular files that have a file extension (such as .flc, .mkv, .avi, .mpg, or anything else) into a new directory. (It isn't clear to me whether you want to move a regular file like "movie.mkv" to "movie.mkv/movie.mkv" or to move it to "movie/movie.mkv". Which do you want?)
  4. You do not want to move (or remove) regular files that do not have a file extension.

Is this correct? (And, if so, do you want new directory names to have the extension stripped off from the names of the files that will be moved to them?)

1 Like

Sir,
despite my wobbly English you have understood everything !!!! :slight_smile:

The files to be moved into a newly created directory should belong only to a defined set of extension. All other extensions should be ignored (for example move.txt will remain where it is).

What I need is the newly created directory to be stripped of the file extension (which luckily appears always to be a dot followed by 3 letters).

This should also cover the theoretical case (I do not seem to have any) of two regular files with the same name but two different extensions (e.g. movie5.mkv and movie5.avi and they should both end up in the same movie5 directory).

I tried whipping up a find command to address the generation of the file list but I am under the impression that the command behaves DIFFERENTLY on Debian and on Mac OS (the two Unices I'm using) :frowning:

Thanks so much for trying to help.

---------- Post updated at 10:47 AM ---------- Previous update was at 09:17 AM ----------

I tried starting this script and have gone somewhere although still not want I want:

#!/bin/sh
find . -type f -maxdepth 1 | while read FILE
do
    DIR=$(echo $FILE | sed 's/\....$//')
    if [ ! -d "$DIR" ]
    then
                echo mkdir "$DIR"
    fi
    echo mv "$FILE" "$DIR"
done

It's not creating the directory if for example the file contains spaces, and therefore the mkdir fails

It's not selecting only the desired extensions

Ciao !

I tested the following using both ksh and bash on Mac OS X even with files named Marvel's Agents of S.H.I.E.L.D..mkv and Marvel's Agents of S.H.I.E.L.D..jpeg and it did what I expected. Having quotes, multiple periods, and spaces in the name seemed like a reasonable test for things you're likely to encounter:

#!/bin/ksh
for i in *.*
do      if [ ! -f "$i" ]        # Verify that we have a regular file...
        then    continue        # if not; skip to next file.
        fi
        dir=${i%.*}             # Strip off the final filename extension.
        if [ ! -d "$dir" ]      # If the target directory is not present...
        then    mkdir "$dir"    # create it.
        fi
        mv "$i" "$dir"          # Move the file into the appropriate directory.
done

As written this would move the above two mentioned files and Marvel's Agents of S.H.I.E.L.D..txt into a directory named Marvel's Agents of S.H.I.E.L.D. . If you really don't want to move *.txt files into the directory with the assoiated movies with the same base name, change the line in the script:

for i in *.*

to

for i in *.flc *.mkv *.avi *.mpg

and add any other filename extensions you want to be processed to the list in the same way. You can put echo in front of the mkdir and mv commands to visually verify that it will do what you want; but, obviously, it won't actually create the directories or move the files as long as those echo commands remain in your script.

Note that this script (and yours) will fail if you have a text file named movie and a file such as movie.mkv because if won't be able to create the directory named "movie" if you already have another file with that name.

1 Like

Thank you so so so so so much :slight_smile:
Take care and whenever you'll need a good recipe for pasta I'll be able to repay my debt :wink:
Robert
Rome - Italy