将 Windows 批处理转换为 Linux 脚本(按第一个字符批量创建符号链接)

将 Windows 批处理转换为 Linux 脚本(按第一个字符批量创建符号链接)

作为 Linux 和脚本的新手,我不确定如何将其转换为 Linux 脚本,甚至不知道从哪里开始。本质上,我想创建一个脚本,创建按字母顺序排列的 AZ 文件夹,将目录中的所有文件夹符号链接到新创建的按 AZ 排序的目录,这样以 A 开头的电影(如 Appleseed)将位于 A 文件夹下。*注意,我尝试在 wine 中运行这个 bat,但 mklink 不存在,效果不太好。

::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"))

答案1

就是这样。语法出奇地相似。我用 .. 缩短了字母表的列表,但如果您愿意,您可以将其列出,它仍然有效。不过,我不确定您的 *nix 机器是否会对这些文件路径感到满意。

#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;

相关内容