How to automatically create folders from file names in Unraid


Recommended Posts

Hi,

 

So I have a bunch of movies stored my Unraid array for Plex Server. Problem is, some of them are in a folder structure (a movie file inside a folder with a same name) and some are just files inside a Movie folder.

Now, there's a batch file in Windows that allows you to automatically create folders from file names and move that file into that folder. This is an excellence tool to organize your movies/tv collection. 

I was wondering if we can do the same in Unraid?

 

Thank you 

Link to comment

This is something I hacked together a while ago. 

 

Remove spaces does what it sounds like because the second half errors out with spaces. 

 

The second part creates a folder and moves the file into the folder. its been a long time since I created this and used this so I don't recall if it'll continue to move files since its not in a loop. Hopefully this will at least get you started. 

 

#!/bin/bash
#noParity=true
#arrayStarted=true

#Remove Spaces from files prior to creating folder
cd /location-of-files
for f in *\ *; do mv "$f" "${f// /.}"; done

#Create Folder from name and move to final location. File types editable below
for FILE in `ls /location-of-files | egrep "mkv|mp4|avi|vob|iso|MKV|MP4|AVI|VOB|ISO"`
do
DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
mkdir /location-of-files/$DIR
mv /location-of-files/$FILE /location-of-files/$DIR

 

Link to comment
42 minutes ago, kizer said:

This is something I hacked together a while ago. 

 

Remove spaces does what it sounds like because the second half errors out with spaces. 

 

The second part creates a folder and moves the file into the folder. its been a long time since I created this and used this so I don't recall if it'll continue to move files since its not in a loop. Hopefully this will at least get you started. 

 

#!/bin/bash
#noParity=true
#arrayStarted=true

#Remove Spaces from files prior to creating folder
cd /location-of-files
for f in *\ *; do mv "$f" "${f// /.}"; done

#Create Folder from name and move to final location. File types editable below
for FILE in `ls /location-of-files | egrep "mkv|mp4|avi|vob|iso|MKV|MP4|AVI|VOB|ISO"`
do
DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
mkdir /location-of-files/$DIR
mv /location-of-files/$FILE /location-of-files/$DIR

 

Thank you! 

I'll try this out

Link to comment
  • 2 weeks later...

Hi, so here's the script that I edited based on your script

Quote

#!/bin/bash
#noParity=true
#arrayStarted=true

#Remove Spaces from files prior to creating folder
cd /mnt/user/Media/Movies/SDR/Test
for f in *\ *; do mv "$f" "${f// /.}"; done

#Create Folder from name and move to final location. File types editable below
for FILE in `ls /mnt/user/Media/Movies/SDR/Test | egrep "mkv|mp4|avi|vob|iso|MKV|MP4|AVI|VOB|ISO"`
do
DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
mkdir /mnt/user/Media/Movies/SDR/Test/$DIR
mv /mnt/user/Media/Movies/SDR/Test/$FILE /mnt/user/Media/Movies/SDR/Test/$DIR

 

And this is the error message I got when trying to run the script

Quote

Script location: /tmp/user.scripts/tmpScripts/move_files/script
Note that closing this window will abort the execution of this script
mv: cannot stat '* *': No such file or directory
/tmp/user.scripts/tmpScripts/move_files/script: line 15: syntax error: unexpected end of file

Any thought?

 

 

 

Link to comment

@caynam

 

Here you go. I messed up. I forgot to re-add the done to complete the loop when I was trying to make the folders generic enough. I should of just left well alone and let you edit/figure it out vs trying to be all slick. lol

 

#!/bin/bash
#noParity=true
#arrayStarted=true

#Remove Spaces-Linux and spaces are no good together
cd /mnt/cache/Media/in/
for f in *\ *; do mv "$f" "${f// /.}"; done

#Create Folder from name and move to final location. File types editable below
for FILE in `ls /mnt/cache/Media/in | egrep "mkv|mp4|avi|vob|iso|MKV|MP4|AVI|VOB|ISO"`
do
DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
mkdir /mnt/user/Media/in/$DIR
mv /mnt/cache/Media/in/$FILE /mnt/user/Media/in/$DIR
done

 

I just tested this in my 

/mnt/Media/in directory

 

I used Transformers the Movie.mkv which was actually a dummy file I created to make sure I'm not deleting the real thing. 

It converted the file from Transformers the movie.mkv to Transformers.the.movie.mkv and then put that in a folder named exactly the same minis the .mkv at the end. 

 

So Transformer the Movie.mkv 

 

to 

 

/Transformer.the.Movie/Transformer.the.Movie.mkv

 

It even worked when I did

Transformers (2007) (720p).mkv

Transformers.(2007.(720p).mkv

  • Like 2
Link to comment
  • 6 months later...
@caynam
 
Here you go. I messed up. I forgot to re-add the done to complete the loop when I was trying to make the folders generic enough. I should of just left well alone and let you edit/figure it out vs trying to be all slick. lol
 
#!/bin/bash#noParity=true#arrayStarted=true#Remove Spaces-Linux and spaces are no good togethercd /mnt/cache/Media/in/for f in *\ *; do mv "$f" "${f// /.}"; done#Create Folder from name and move to final location. File types editable belowfor FILE in `ls /mnt/cache/Media/in | egrep "mkv|mp4|avi|vob|iso|MKV|MP4|AVI|VOB|ISO"`doDIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`mkdir /mnt/user/Media/in/$DIRmv /mnt/cache/Media/in/$FILE /mnt/user/Media/in/$DIRdone

 
I just tested this in my 
/mnt/Media/in directory
 
I used Transformers the Movie.mkv which was actually a dummy file I created to make sure I'm not deleting the real thing. 
It converted the file from Transformers the movie.mkv to Transformers.the.movie.mkv and then put that in a folder named exactly the same minis the .mkv at the end. 
 
So Transformer the Movie.mkv 
 
to 
 
/Transformer.the.Movie/Transformer.the.Movie.mkv
 
It even worked when I did
Transformers (2007) (720p).mkv
Transformers.(2007.(720p).mkv


Hello Kizer, I am new to Unraid, Dockers and etc., I would like to do this as I am in the same situation. I built a new Unraid server for Plex. How do I go about doing this? Is it thru Dockers, command prompt from within Unraid, script or what exactly? Can you guide me step by step on how it’s done? Thanks in advance


Sent from my iPhone using Tapatalk
Link to comment

@Beat-O

 

OH sorry. Its just using some code that I posted up above and User.Scripts Plugin so you can either schedule the script to run or click on it to run it manually. You'll find plugin in the Community Applications App. 

 

You can simply take the text with the above code and make a bash script just make sure you make it executable so it runs on your system. I just find running things via User.Scripts the easiest. 

 

I also made a few changes so you just have to plug in a few values vs all of your paths. Also I threw in another option to move it to a final location just incase where your originally had the files are not the final location. 

 

Also added in some Chmod magic in case you want your files not to be root which this script make them be. 

 

https://pastebin.com/XaWP98tM

 

 

  • Like 1
Link to comment
@Beat-O
 
OH sorry. Its just using some code that I posted up above and User.Scripts Plugin so you can either schedule the script to run or click on it to run it manually. You'll find plugin in the Community Applications App. 
 
You can simply take the text with the above code and make a bash script just make sure you make it executable so it runs on your system. I just find running things via User.Scripts the easiest. 
 
I also made a few changes so you just have to plug in a few values vs all of your paths. Also I threw in another option to move it to a final location just incase where your originally had the files are not the final location. 
 
Also added in some Chmod magic in case you want your files not to be root which this script make them be. 
 
https://pastebin.com/XaWP98tM
 
 

Ok, thanks Kizer, I will give it a try to the best I can, the last part of your reply, lost me because I don’t know what that means, as the original post, am new to Unraid/Linux and etc., can follow instruction really well but something I don’t understand. Thanks again brother


Sent from my iPhone using Tapatalk
Link to comment

@Beat-O

 

You need to make sure you have community Applications installed. Which will give you an app store. 

 

From there you want to install a Plugin called User.Scripts

 

From there you simply post in the code I gave you and make your subtle changes and click the run button. 

 

Community Applications is a must Plugin to have because it gives you a really cool store to install programs. 

 

Don't worry just do your thing and I'll answer any questions as you come up with them. 😃

 

The Pastebin link I posted up is a modified version of the code to help simply things. Just fire over questions as you need to and I'll do my best to keep answering them. 

  • Like 2
Link to comment
2 hours ago, Beat-O said:

Quick question, I am routing my directory paths, if a directory of mine has a “space”, do I use underscore or dash or enter it exactly with the space? Thanks


Sent from my iPhone using Tapatalk

 

Personally I always use . for spaces. Just keep in mind in the path you must use it exactly like it is. You could always change your path from Output files to Output.files and then change it back after you run this. Normally you wrap spaces in " " for example "Output files" so linux knows there is a space. I don't know if this will support it. I'll give it a try later when I can.  

Link to comment
21 minutes ago, kizer said:

 

Personally I always use . for spaces. Just keep in mind in the path you must use it exactly like it is. You could always change your path from Output files to Output.files and then change it back after you run this. Normally you wrap spaces in " " for example "Output files" so linux knows there is a space. I don't know if this will support it. I'll give it a try later when I can.  

 

Ok thank you so much.

One more question, so I got it to work but how do I go about it if I wanted the folder to be created with space (like at least the same as the file name) or with name and  year only? example of file name layout: 


Sample Movie (2023) WEBRip-1080p.mp4    to folder: Sample Movie (2023)
Movie (2022) Bluray-1080p.mp4   to folder: Movie (2022)

 

Thanks in advance

Link to comment
18 hours ago, kizer said:

So your wanting to prune the title or something different? Can you give me some exact examples. I'm confused in what you're showing me up above. 

Hello Kizer, hope you had a good day, just got home from work, had a long day. Okay I am going to explain, my files are sort of the way I want them, am going to show you the way they are currently in the following 2 samples (not original titles but its samples), NOTE: "Sample Movie" & "Movie" is a representation of a movies title or a videos to show the format I have the files:

 

Sample Movie (2023) WEBRip-1080p.mp4

Movie (2022) Bluray-1080p.mp4

 

From those files, I would like the script to either create the folders based on titles (without removing spaces or adding "." periods) or if its possible to just create the folder with name and year, example as follows (same as above apply, "Sample Movie" & "Movie" are titles of the movies or videos):

 

Sample Movie (2023)

Movie (2022)

 

Hope you understand, Thank you buddy

Edited by Beat-O
Link to comment

So basically you just want folders created from files with spaces. Or I should say just put the file in the folder. as it is.  I'll take a look at that. At the time I created this script I was having problems with Spaces and needed a utility. 

 

Otherwise if you want some crazy naming convention you could give Filebot a try, but you would have to modify a file to your liking which can kinda be a pain in the butt, but I could share my config file if you really wanted the year and other info in it. https://www.filebot.net/

There is a command line version of it that just runs when you drop files into it. 

Link to comment
So basically you just want folders created from files with spaces. Or I should say just put the file in the folder. as it is.  I'll take a look at that. At the time I created this script I was having problems with Spaces and needed a utility. 
 
Otherwise if you want some crazy naming convention you could give Filebot a try, but you would have to modify a file to your liking which can kinda be a pain in the butt, but I could share my config file if you really wanted the year and other info in it. https://www.filebot.net/
There is a command line version of it that just runs when you drop files into it. 

Yes folders created from files name. Yes put the files in the folder, and was saying instead of removing the space if it was possible to remove “webrip” and “Blu-ray” from the folder when created.

I checked out filebot before coming across this forum, gave it a try, didn’t understand it, it seemed it’s just to rename files, but honestly I like the naming structure I currently have, I just need to create the folder, drop the file inside and that would be good enough.

What’s going on is that I have Handbrake to covert all my media, the media is already on its folder exactly how I need them, but when Handbreak finish the conversion it’s just a bare file and no folder, I have to manually drop each file one by one to the original folder and then delete the old file, trying to do everything automated. Hope you understand


Sent from my iPhone using Tapatalk
Link to comment

Have you looked at Tdarr? It does all of that automatically. It will convert right where your files are and require literally no input from you after everything is setup. Its pretty neat actually. 

 

As far as Filebot goes you can create custom Naming conventions and it will stick to them. That's why I said I can share my config file with you because I have my files set up a specific way too. Easy Peasy. 

Link to comment
Have you looked at Tdarr? It does all of that automatically. It will convert right where your files are and require literally no input from you after everything is setup. Its pretty neat actually. 
 
As far as Filebot goes you can create custom Naming conventions and it will stick to them. That's why I said I can share my config file with you because I have my files set up a specific way too. Easy Peasy. 

I haven’t tried Tdarr, I have Jacket, Sonarr and Radarr, with Handbrake and DelugeVPN.

I can give filebot a try, whenever you can, share it with me please.


Sent from my iPhone using Tapatalk
Link to comment

Here you go... Create Folders from files with Spaces. Keep in mind this version will process any and all files in a set directly. I didn't build in a Filter to look for specific tile types this time. I might update it later if I get the time. 

 

https://pastebin.com/PAkZXNCW

 

This is for anybody else who finds this Topic and wants the use of it. Crazy how things popup months or years later after you post something up. 

 

  • Like 1
Link to comment
  • 4 weeks later...

This is the script i used.

 

Make sure to replace "/path/to/files" with the actual directory path where your files are located.

 

 

 

#!/bin/bash

 

# Set the directory where the files are located
files_directory="/path/to/files"

 

# Change to the files directory
cd "$files_directory" || exit

 

# Iterate over each file in the directory
for file in *; do
  # Check if the item is a file (not a directory)
  if [[ -f $file ]]; then
    # Extract the file name without the extension
    file_name=$(basename "$file")
    file_name_without_extension="${file_name%.*}"

 

    # Create a folder with the same name as the file (if it doesn't exist)
    mkdir -p "$file_name_without_extension"

 

    # Move the file into the newly created folder
    mv "$file" "$file_name_without_extension"
  fi
done
 

 

Link to comment
  • 7 months later...
On 6/16/2023 at 8:36 PM, Kay88 said:

This is the script i used.

 

Make sure to replace "/path/to/files" with the actual directory path where your files are located.

 

 

 

#!/bin/bash

 

# Set the directory where the files are located
files_directory="/path/to/files"

 

# Change to the files directory
cd "$files_directory" || exit

 

# Iterate over each file in the directory
for file in *; do
  # Check if the item is a file (not a directory)
  if [[ -f $file ]]; then
    # Extract the file name without the extension
    file_name=$(basename "$file")
    file_name_without_extension="${file_name%.*}"

 

    # Create a folder with the same name as the file (if it doesn't exist)
    mkdir -p "$file_name_without_extension"

 

    # Move the file into the newly created folder
    mv "$file" "$file_name_without_extension"
  fi
done
 

 

 

Thank you for working this out. It successfully retained spaces and special characters especially for my french content.

 

DiscGolferPro

 

MoveToFolder-script.png

Link to comment
  • 2 months later...

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.