How to run jpegoptim recursively for each file in a directory

This command will run jpegoptim in lossless mode for each .jpg file in a folder recursively. This is how I reduce the size of my photo & image collection.

find . \( -iname \*.jpg -or -iname \*.jpeg \) -print0 | xargs -P 4 -n 1 -0 jpegoptim

See How to use xargs in parallel for details on  the -P 4 -n 1 syntax which runs 4 jpegoptim processes in parallel.

Note that the output may be scrambled due to 4 processes running in parallel, but it’s typically 4 times faster using this approach. You can also use

find . \( -iname \*.jpg -or -iname \*.jpeg \) -print0 | xargs -0 jpegoptim

to run just one jpegoptim process in parallel.