Quickly Make Directories Based on Files in Location

I needed to make a bunch of folders for a slew of singles I’d downloaded.  I wanted each single to have its own folder but when you download singles they tend to offer just the flac file and no containing folder.  Reasonable, I suppose.

Anyway, I wrote this to create the folders.

for f in ./* ; do ff=$( printf '%s\n' "${f//.\//}" ) ; mkdir "${ff//.flac/}" ; done 

Good enough.  I could have added something to move each file (“${f}”) into the newly created folder (./”${ff}”) but didn’t think of that until I was done.  Probably an addition like this (untested):

for f in ./* ; do ff=$( printf '%s\n' "${f//.\//}" ) ; mkdir "${ff//.flac/}" ; mv "${f}" "./${ff}/${f}" ; done 

Enjoy! Maybe I’ll get around to testing the move version at some point.


Leave a Reply

Your email address will not be published. Required fields are marked *