Command to move all mkv files up a folder


gnollo

Recommended Posts

Hi there,

Just finished converting my 1000+ old dvds to mkvs with makemkv, it placed each file in the VIDEO_TS folder.

I would like to find a command that 

- moves up each mkv to the parent folder

Once that is done I want to backup the VIDEO_TS folders to a backup folder retaining the parent folder name.

Basically I want to separate the two, so I can add to emby the folders containing the MKV files.

Each parent folder also contains the xml file which emby uses to identify the movie, I'd like to keep that too.

Thank you guys for your help in advance!

 

Link to comment

Here is a script that should do what you need.  Set "movies" and "backup" to your folders.

move_movies.sh

#!/bin/bash

movies="/mnt/user/test/movies"
backup="/mnt/user/test/backup"

process()
{
    local movieDir=$1
    local backupDir=$2
    
    pushd "$movieDir" >/dev/null
        find ./ -iname "*.mkv" -exec mv -f {} . \;
        mkdir -p "$backupDir"
        mv -f AUDIO_TS VIDEO_TS "$backupDir"
    popd >/dev/null
}

ls -1d "$movies"/*/ | sed 's/\/*$//' |\
while read movie
do
    name="$(sed 's/.*\///g' <<< $movie)"
    back="$backup/$name"

    printf "$name\n"

    if [ -d "$movie/DVD1" ]
    then
        ls -1d "$movie"/DVD*/ | sed 's/\/*$//' |\
        while read dvd
        do
            num="$(sed 's/.*\///g' <<< $dvd)"
            process "$dvd" "$back/$num"
        done
    else
        process "$movie" "$back"
    fi
done

 

Edited by JoeUnraidUser
Link to comment

Thank you Joe! I checked one more time and it is slightly more complex than what I stated.

The program created an additional folder in each VIDEO_TS file, which adds underscores if the folder name has multiple words in it. Examples:

\\tower\unraid\dvds\forrest gump\VIDEO_TS\FORREST_GUMP

\\tower\unraid\dvds\everything you wanted to ask about sex\VIDEO_TS\EVERYTHING_YOU_WANTED_TO_ASK_ABOUT_SEX

 

Link to comment
On 9/22/2019 at 6:51 PM, gnollo said:

Thank you Joe! I checked one more time and it is slightly more complex than what I stated.

The program created an additional folder in each VIDEO_TS file, which adds underscores if the folder name has multiple words in it. Examples:

\\tower\unraid\dvds\forrest gump\VIDEO_TS\FORREST_GUMP

\\tower\unraid\dvds\everything you wanted to ask about sex\VIDEO_TS\EVERYTHING_YOU_WANTED_TO_ASK_ABOUT_SEX

I modified the script to move "mkv" files from anywhere under the movie folder.

Edited by JoeUnraidUser
Link to comment
23 hours ago, gnollo said:

I've never run a script on unraid before, how do I do that (I mostly still a windows guy)?

  1. Install the "CA User Scripts" app.
  2. Go to the settings tab and click on the "User Scripts" icon.
  3. Click on "ADD NEW SCRIPT".
  4. Give the script a name.
  5. Click on the name of the script.
  6. Select "Edit Script".
  7. Paste your script and make any changes.
  8. Hit "SAVE CHANGES".
  9. To the right of the name click "RUN SCRIPT".

 

Edit:  I would test it first on a couple movies.  For example:

test
├── backup
└── movies
    ├── everything you wanted to ask about sex
    └── forrest gump

 

Edited by JoeUnraidUser
Link to comment
52 minutes ago, gnollo said:

Ok, thank you again Joe, I am all set. How do I know that the script will not affect any other folder (containing another 3500 movies) than the one where all the DVDs are set? I moved them to a different location and left a few behind in the DVD folder, to perform a test run.

It will only affect the folders under the folders you define with the variables "movies" and "backup"

Edited by JoeUnraidUser
Link to comment
On 9/23/2019 at 6:57 PM, gnollo said:

So if I create these folders

\\tower\test

\\tower\test\movies

\\tower\test\backup

drop some test dvds in the movies folder, I can run the script to test it?

Yes.

 

Note:

1. On Linux, slash / is used for folders instead of the Windows backslash \

2. On Unraid, the share \\tower\test is represented on Linux as /mnt/user/test

3. Linux folder names are case sensitive.

 

So your folder names in Linux should look like this:

movies="/mnt/user/test/movies"
backup="/mnt/user/test/backup"

 

Edited by JoeUnraidUser
Link to comment

Ok, this is the message that appeared during the run.

 

Script location: /tmp/user.scripts/tmpScripts/mkv dvd fixer/script
Note that closing this window will abort the execution of this script
CATCH_AND_RELEASE
Cary Grant, A Class Apart
casino royale
casino
casshern
castaway
catch me if you can
mv: cannot stat 'VIDEO_TS': No such file or directory

Link to comment

Worked perfectly. One of the folders didn't have a VIDEO_TS folder, which is why it gave the error message. One last thing would be to also move across the AUDIO_TS folder? You are an absolute gem Joe this is going to save me a ton of time! And if anything goes wrong I can always fix it quickly as I have the original files in the backup folder! This is ace!

Link to comment
4 minutes ago, gnollo said:

Worked perfectly. One of the folders didn't have a VIDEO_TS folder, which is why it gave the error message. One last thing would be to also move across the AUDIO_TS folder? You are an absolute gem Joe this is going to save me a ton of time! And if anything goes wrong I can always fix it quickly as I have the original files in the backup folder! This is ace!

I changed the script so it also moves AUDIO_TS to the backup.

Link to comment

Also noticed that in one of the folders I had two subfolders (DVD1 and DVD2). The script seems to have copied the two mkv files over each other so I only have one mkv file in that specific movie folder. No biggie, is there is a way to identify which folders have subfolders once the merge is done and I can rectify that manually by recreating the missing mkv.

Link to comment
I can change the script to accommodate for DVD# subfolders.  It will take another day to make the changes.  What is the structure under the DVD# folders.  For example, does it have a VIDEO_TS and AUDIO_TS under DVD1?
No need, it will only be a handful, I'll do a search for dvd in the folders adds identify which have subfolders, and fix those first manually moving the mkv files.

Sent from my SM-A520F using Tapatalk

Link to comment

Run it on the folder, went through all a to z and completed

some of the error messages

mv: cannot stat 'AUDIO_TS': No such file or directory

rev: stdin: Invalid or incomplete multibyte or wide character

mv: cannot move 'VIDEO_TS' to '/mnt/user/unraid/dvdbackup/VIDEO_TS': Directory not empty

mv: './Charlie And The Chocolate Factory-1.mkv' and './Charlie And The Chocolate Factory-1.mkv' are the same file

mv: cannot stat 'VIDEO_TS': No such file or directory

I will investigate them one by one tomorrow

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.