History always repeats itself and sometimes that's good. Here's a second chance to be part of it: tmdtc.com

Saturday 5 January 2008

Batch operation adding a filename suffix

I wanted to resize all sponsors logos on the Profoss website. The file name is of the form logo_company.ext, and I wanted to rename it to something like logo_company.small.ext so I didn't loose the original files. The trick here is to split the filename in 2 parts: filename without the extension and the extension.

This is easy with bash, as it lets you manipulate strings quite easily. For example, if the complete filename is stored in the variable $f, the filename without extension is obtained with ${f%.*}, and the extension with ${f:(-3)} (my file extensions where always 3 characters long ;-)

With the convert tool from imagemagick, it was a piece of cake to get the expected result. Here's the command:

1 comment:

Anonymous said...

Getting the extension as the last piece of text separated by a dot can be done with ${f##*.} also.