This question already has an answer here:

I have a problem! I have a bunch of image files with extensions. Jpg and. Png. I wrote a script that renames the files, changing their names in a pattern followed by a number like this:

... arquivo_1.jpg arquivo_2.jpg ... And following this sequence, but it needs to know the extension, in this case I used the extension. Jpg.

I was wondering how do I optimize the script so that it renames but keeping the original extension of the file and keep doing the sequence I described above. Can someone help me?

marked as duplicate by jasonwryan, peterph, Anthon, slm, vonbrand Mar 6 '14 at 21:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


if you have a filename in a variable: file=photo.jpg

You can get the extension like this: ext=${file##*.}

That removes, from the beginning of the string, all character up to and including the last dot:

$ echo $ext

See http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion

Not the answer you're looking for? Browse other questions tagged or ask your own question.