Automate arranging files into folders based on beginning of file name? Looking to quickly arrange a whole load (talking about multiple thousands) of files into subfolders, and the files come in sets of three (left audio, right audio, video) separate files. Not experienced in terminal really but messed around in Automator before.

Example:

00015_A01.mxf
00015_A02.mxf
00015.mxf
00016_A01.mxf
00016_A02.mxf
00016.mxf


I want to automate it so that Finder looks for files with matching first characters, and puts them in a sub folder (preferably with the name of that file but I’ve managed a workaround for that.)

So the result would be:

[00015] (folder)
00015_A01.mxf
00015_A02.mxf
00015.mxf

[00016] (folder)
00016_A01.mxf
00016_A02.mxf
00016.mxf


Any ideas? I checked around on here but the answers were a bit over my head...
for i in {10..99}
do
    mkdir 000$i
    mv 000$i* 000$i
done

for i in {100..999}
do
    mkdir 00$i
    mv 00$i* 00$i
done

for i in {1000..9999}
do
    mkdir 0$i
    mv 0$i* 0$i
done


This bash script should work for 10-9999 directories.