I have files with filenames such as:
text_2_2022-11-22_K03_10k_66_P578_B06_2999dl_text_693_coff_442.jpg text_2_2022-11-22_K03_10k_66_P578_B06_2999dl_text_693_coff_432.jpg text_2_2022-11-22_K05_10k_66_P578_B06_2999dl_text_693_coff_175.jpg text_2_2022-11-22_K05_10k_66_P578_B06_2999dl_text_693_coff_832.jpg text_2_2022-11-22_K08_10k_66_P578_B06_2999dl_text_693_coff_322.jpg text_2_2022-11-22_K08_10k_66_P578_B06_2999dl_text_693_coff_975.jpg
I want to create subfolders like:
K03_10k_66_P578_B06
K05_10k_66_P578_B06
K08_10k_66_P578_B06
and then move the files to the correct subfolder.
Other answers use parameter expansion but I could not figure out to do this given the more complex names. Therefore I wrote a grep/regex command that matches the needed part of the filename but could not implement it in a loop:
grep '[A-Z][0-9]*_[0-9]*[a-z]_[0-9]*_[A-Z][0-9]*_[A-Z][0-9]*'
I have found similar answers (1 2 3 4) to my question but my filenames are more complex than in those questions.
grep
isn't for operating via the names of files. It's for printing data from the contents of files based on regular expressions..jpg
suffix? And you want to use the 4th through the 8th field to construct a subfolder name, create that subfolder, and move the file into that subfolder?