After hours of playing with bash, I found a way how Google is doing it. It is using one of these 2 programs:
Both of them support lossless and lossy conversion. I tried the second one, in Ubuntu as jpegoptimjpegoptim Install jpegoptim http://hostmar.co/software-small
.
Than to do what I want I have to do the following:
find MyDirectory/ -type d -exec sh -c ' ls "$0"/*.jpg 2>/dev/null && jpegoptim --strip-all -t "$0"/*.jpg ' {} \; This is doing really close to what I want. The only problem is that I see a lot of output about each file compression and I can not get rid of it with -q because it also get rid of summary. But this is the closest I was able to get with my just learned bash "skills". I asked a follow-up question on Ask Ubuntuasked a follow-up question on Ask Ubuntu.
It is really fast. It took it < 5 sec to process few thousands of files.