Advertisement
Available-Elevator69

Create Folder from File name Remove Spaces with Variables

May 22nd, 2023 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.98 KB | None | 0 0
  1. #!/bin/bash
  2. #noParity=true
  3. #arrayStarted=true
  4.  
  5. ## This script will create folders from files. First it will remove
  6. ## all spaces and create a folder from the file name
  7. ## It will also move files/directories to final location if you uncomment down in the code
  8. ## It will also change your file permissions from root to nobody uncomment below to use
  9. ## Adjust your directories below for your needs.
  10. ## Have Questions ask kizer on unraid forum
  11. ## All code has been cobbled together by me and tested on my own system to insure it works
  12.  
  13. ## Source Directory of Files, change below to your path. I left mine in for example
  14. FROM=/mnt/cache/uploads/Tv-Incoming
  15.  
  16. ## Script will leave renamed files where they are unless you comment and adjust below
  17. ## Keep in mind you have to uncomment below and the 2nd to last line to work
  18.  
  19. ## Only if you want to move new folder to another location. Change below to your path
  20. ## and uncomment near the very bottom of this script
  21. NEWLOCATION=/mnt/cache/Media/Movies
  22.  
  23. ## Remove Spaces-Linux and spaces are no good together
  24. cd $FROM/
  25. for f in *\ *; do mv "$f" "${f// /.}"; done
  26.  
  27. ## Create Folder from name and move to final location. File types editable below.
  28. ## If File type is not listed below it will not be moved or renamed
  29. for FILE in `ls $FROM/ | egrep "mkv|mp4|avi|vob|iso|MKV|MP4|AVI|VOB|ISO"`
  30. do
  31. DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
  32.  
  33. ## Make Directory
  34. mkdir $FROM/$DIR
  35.  
  36. ## Move File into new Directory
  37. mv $FROM/$FILE $FROM/$DIR
  38.  
  39. ######################## OPTIONAL BELOW #################
  40. ## Remove Pound symbol option you would like to use
  41. ## Change Permissions so Everything is Happy
  42.  
  43. ## Changes from read write from ROOT
  44. #chmod -R u-x,go-rwx,go+u,ugo+X $FROM
  45. ## Changes owner from ROOT to Nobody which is pretty standard on unraid
  46. #chown -R nobody:users $FROM
  47.  
  48. ## Move Directory to Final Place. Leave Comment if this is the final place
  49. #mv $FROM/$DIR $NEWLOCATION
  50.  
  51. ###################### DONE OPTIONAL ####################
  52.  
  53. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement