4

I'm running this command to create thumbnails with mogrify and it works great!

#! /bin/bash
 mogrify             \
-resize 300x300     \
-crop 200x200+0-20 \
-gravity center   \
-format jpg       \
-quality 100       \
-path thumbs      \
 *.jpg

But what I would like to add is a suffix to the output filenames like. -avatar So the output image name is changed from testimage.jpg to testimage-avatar.jpg.

Thanks all!

Mensur
  • 457
  • 9
  • 28
  • Try with `convert` : http://superuser.com/questions/597428/how-can-i-run-mogrify-but-prefix-the-filename. – SLePort Dec 17 '16 at 09:57
  • Hi, saw now that I need to use convert to change the name. Now I just need to figure it how to add a suffix instead of prefix. – Mensur Dec 17 '16 at 10:02

1 Answers1

10

I don't think you can do it with mogrify, but you can use convert:

convert result.png -set filename:new '%t-avatar' %[filename:new].jpg

and you would have to put it in a loop over all JPEG files:

shopt -s nullglob
for f in *.jpg; do
   convert "$f" -set filename:new "%t-avatar" "%[filename:new].jpg"
done

Alternatively, you could retain your original mogrify command - which is actually more efficient than convert and then go into the thumbs directory afterwards and use rename to put the avatar bit in:

rename --dry-run -X --append="-avatar" *.jpg

Sample Output

'a.jpg' would be renamed to 'a-avatar.jpg'
'image.jpg' would be renamed to 'image-avatar.jpg'
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Worked like a charm with rename :) – Mensur Dec 17 '16 at 10:19
  • Not sure if this is intended for Windows but on Windows 10, for the rename command it returns `The syntax of the command is incorrect.` – Adam Jagosz Jan 22 '21 at 19:46
  • @AdamJagosz Correct, it is not for Windows - the question was tagged `linux` and `bash`. Sorry. – Mark Setchell Jan 22 '21 at 20:02
  • 1
    @MarkSetchell Right... I ran this in my WSL (Ubuntu), installed `rename` and got ```Unknown option: dry-run Unknown option: X Unknown option: append``` – Adam Jagosz Jan 22 '21 at 23:03
  • @AdamJagosz There are several packages with a `rename` utility. The one you want is also referred to as `prename`, `Perl rename` or `Larry Wall rename` as it's a Perl script. – Mark Setchell Jan 23 '21 at 08:36