Ubuntu – Resize multiple files and rename them properly

command line

I have a command line problem – probably a pretty easy one, but it seems I can't enter the right words into google.

So I want to resize all *.png images in the folder using imagemagick – this would be something like "convert -resize 80% " and I want "filename" → "small_filename"

So I tried:

for f in *.png ; convert -resize 80% "$f" "small_$f" ; done

but "syntax error near unexpected token `convert" – I suppose you can't just import imagemagick commands into the bash ?

I would be delighted if you could help.

Best Answer

  • You missed do after for ... string. Here is a slightly modified script, but your version will work also if you add do missing in there.

    for f in *.png
      echo "Converting $f."
      convert "$f" -resize 80% "${f/.png/-80%.png}"

    A name changing here works as follows: for every $f as a text string find its .png part and change it for -80%.png

  • Related Question