I have got strings like tran_crossings_exp_.txt
and tran_buildings_exp_.shp
How can I write a bash function to test if the last character before the dot extension starts with an underscore and replace the underscore to have a name like tran_crossings_exp.txt
I know how to get the basename and extension of the file in bash
I have done this solution but I am seeking a more elegant way which also check if the file indeed has an underscore and skips it if the name already is proper.
for file in `ls *_.*`;do ext="${file##*.}"; filename="${file%.*}";non=${file::-5}; mv ${file} ${non}.${ext};done