0

As a noob to Linux and scripting I'm unsure as to how to convert this into a linux script or even where to start. Essentially I want to create a script that creates alphabetized folders A-Z, symlink all folders in a directory to a the newly created sorted A-Z Directory so movies starting with A like Appleseed would be under the A folder. *note I tried just running this bat in wine but mklink doesn't exist and wouldn't work so well.

::Make folders with each letter of the alphabet under the "categorized" folder if not already created
For %%M in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (mkdir "C:\My Videos\Categorized\"%%M)
::Does a directory search matching every letter A-Z and creates a output file listing each folder beginning with that letter 
For %%N in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ((Dir "G:\My Videos\Movies\"%%N* /b) >> "C:\My Videos\List\%%N.lst") 
::Uses the previously created files to create symlinks of each line in the listed files into the alphabatized folders each A-Z folders
For %%O in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (For /F "usebackq delims==" %%P in ("C:\My Videos\List\%%O.lst") do (mklink /d  "G:\My Videos\Categorized\%%O\%%P" "G:\My Videos\Movies\%%P"))
  • “I'm unsure … where to start.” Start by typing man bash. Proceed to writing simple scripts. Translate your batch file to a shell script one command at a time. – Scott Oct 12 '13 at 3:12
  • can you please help me on this stackoverflow.com/questions/21403772/… – Goofy Feb 1 '14 at 12:39
2

Here you go. The syntax is surprisingly very similar. I shortened the listing of the alphabet with .. but you could list it out if you want and it would still work. I'm not sure if your *nix box is going to be happy about those file paths though.

#Make folders with each letter of the alphabet under the "categorized" folder if not already created
for M in {A..Z}; do mkdir "C:\My Videos\Categorized\${M}"; done;
#Does a directory search matching every letter A-Z and creates a output file listing each folder beginning with that letter 
for N in {A..Z}; do ls "G:\My Videos\Movies\${N}"* >> "C:\My Videos\List\${N}.lst"; done;
#Uses the previously created files to create symlinks of each line in the listed files into the alphabatized folders each A-Z folders
for O in {A..Z}; do for P in $(cat "C:\My Videos\List\${O}.lst"); do ln -s  "G:\My Videos\Categorized\${O}\${P}" "G:\My Videos\Movies\${P}"; done; done;
#This alternate version for the last line correctly deals with whitespace in your filenames
for O in {A..Z}; do 
    while read P; do 
        ln -s  "G:\My Videos\Categorized\${O}\${P}" "G:\My Videos\Movies\${P}";
    done < "C:\My Videos\List\${O}.lst";
done;
  • The last part doesn't seem to work for me I'm using Ubuntu mint flavor 14 nadia. Specifically the symlink is pinking up whitespace in the /media/Movies/My Videos/List/A.lst file I'm using this line for O in A; do for P in $(cat "/media/Movies/My Videos/List/$O.lst"); do ln -s "/media/Movies/My Videos/Movies/${P}" "/media/Movies/My Videos/Categorized/${O}/${P}"; done; done; this is the result ln: failed to create symbolic link /media/Movies/My Videos/Categorized/A/The': File exists ln: failed to create symbolic link /media/Movies/My Videos/Categorized/A/[2011]"': File exists – Thomas Oct 13 '13 at 7:57
  • Didn't realize you have whitespace in your filenames. Use this: for O in {A..Z}; do while read P; do ln -s "G:\My Videos\Categorized\${O}\${P}" "G:\My Videos\Movies\${P}"; done < "C:\My Videos\List\${O}.lst"; done; – Michael Yasumoto Oct 13 '13 at 17:56
  • can you please have a look the link and help me ? stackoverflow.com/questions/21403772/… – Goofy Feb 1 '14 at 12:39

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.