I have a folder containing files of the pattern shown below. The plan is to loop through every file name, and match everything between the last / and the . which would match the species name plus underscore in between. But I cannot seem to figure out the right regex. After the match works, I would like to save the file under a new name, where _sort is appended to the original file name.
The files to loop through
/home/directory/escherichia_coli.spart
/home/directory/pseudomonas_aeruginosa.spart
/home/directory/pseudomonas_fluorescens.spart
/home/directory/streptocuccus_agalactiae.spart
My most promising attempt:
for i in /home/directory/*.spart; do
$i > $i/${i: /$.*\.}_sort.txt; done
/$
to match the last / of the line
.*
match everything after the last /
\.
match the dot