Ok so you want to make your website load faster, or maybe free up some disk space? You don't want to sacrifice image quality or waste to much time messing around to do it./ maybe you have 500 websites to do at once? Lets take care to preserver the file permissions too!
For JPEG let use mozcjpeg from Mozilla https://github.com/mozilla/mozjpeg
once installed follow this example, it will go through all folders and subfolders and in-place compress every jpeg losslessly! Of course you can even change the parameters to do some lossy compresion too but here we don't want to sacrifice quality
find /your/folders -type f -iname '*.jpg' -exec sh -c 'nice -n 19 ionice -c2 -n7 mozcjpeg -outfile "{}.out" -optimize "{}"; echo doing "{}";chmod --reference "{}" "{}.out"; chown --reference "{}" "{}.out"; touch -r "{}" "{}.out" ;mv "{}.out" "{}" ' \;
For PNG, use similar via pngcrush
find /your/folders -type f -iname '*.png' -exec sh -c 'nice -n 19 ionice -c2 -n7 pngcrush -q -brute "{}" "{}.out"; echo doing "{}";chmod --reference "{}" "{}.out"; chown --reference "{}" "{}.out"; touch -r "{}" "{}.out" ; mv "{}.out" "{}" ' \;