Ubuntu – JPG batch compression & rename (find -exec, xargs, piping?)

command lineconvert-commandfind

this is my first post so I hope you'll be kind enough to forgive me if I make some formatting mistakes or else. This is one question I've had for quite some time and while looking for answers I have more questions arising. I was trying to find a way to compress pictures in a folder using convert in one command.

First I simply tried:

convert * -resize 50% Pic.jpg

However this returned the compressed pictures named Pic-0.jpg, Pic-1.jpg, etc. (I also tried $(find ".JPG") instead of * but this is expanded to the same. In order to have the formatting I wanted I tried:

convert * -resize 50% Pic_lorez_{1..3}.jpg

But this seemed to take the Pic_lorez_2.jpg and Pic3_lorez_3.jpg as input files, not output files. So following the advice of a friend, I tried:

find . -name "*.JPG" -exec convert {} -resize 50% {} \;

This compress all files under the same name but doesn't allow to put the name I want to these files (for example, Pic_lorez_{1,2,3,4,5,..100}.jpg).

Would xargs make it possible? Something along the lines of:

find . -name "*.JPG" | xargs convert ???

I couldn't figure out the proper syntax with xargs. I finally tried a direct pipe of find into convert but convert didn't seem to be able to work that way.

Best Answer

convert writes to a different image file. To overwrite original image files use mogrify.

Single file:

mogrify -resize 50% Pic.jpg 

All .jpg files:

mogrify -resize 50% *.jpg