Terminal script to create folders and copy files based on part of filename I have a folder with thousands of files in this format


filenameA - 1.ext  
filenameA - 2.ext  
filenameB - 1.ext  


and sometimes


filenameB - 02.ext  


I need a script that will read the files and create a single folder with each distinct filename before the " - #.ext" and then add all files with that same name into the folder.

From the example above I would have two folders 1) filenameA 2) filenameB -
 each with two files in them. The actual data has thousands of files and each group can have 1 to 50ish files, with sequential numbers added to the end.
If all your files follow the convention of dirname - the_rest_and_ext (have the - in the middle) you can use something like:

for f in *; do
   dir_name=`echo "$f" | sed 's/ -.*//'`
   mkdir -p "$dir_name"
   mv "$f" "$dir_name"
done


If all the files have the same extension, you can change the for to something like:

for f in *.ext; do


NOTE: make sure you know what you are doing.