views:

45

answers:

2

I've thousands of png files which I like to make smaller with pngcrush. I've a simple find .. -exec job, but it's sequential. My machine has quite some resources and I'd make this in parallel.

The operation to be performed on every png is:

pngcrush input output && mv output input

Ideally I can specify the maximum number of parallel operations.

Is there a way to do this with bash and/or other shell helpers? I'm Ubuntu or Debian.

+5  A: 

You can use xargs to run multiple processes in parallel:

find /path -print0 | xargs -0 -n 1 -P <nr_procs> sh -c 'pngcrush $1 temp.$$ && mv temp.$$ $1' sh

xargs will read the list of files produced by find (separated by 0 characters (-0)) and run the provided command (sh -c '...' sh) with one parameter at a time (-n 1). xargs will run <nr_procs> (-P <nr_procs>) in parallel.

Bart Sas
`$1` does not get populated, I also tried it with a minimal example without luck. xargs is 4.4.0, any idea?
mark
I forgot to specify the value for $0. Should be fixed now.
Bart Sas
@Bart: confirmed, works! Could you please edit your answer too?
mark
A: 

Two options:

  1. parallel from package moreutils
  2. GNU parallel

I think Debian/Ubuntu does not provide offical packages for GNU parallel, so you can try moreutils first.

tokland

related questions