1

I would like to run optipng in parallel with my 8-thread CPU using the shell.

I know the program itself is not multicore optimized, the only way will be to run 8 files with it.

I have 500+ PNG images only in my home directory, and I'd like to do the job as fast as possible.

I am likely asking a question that already has been answered, but did not find it.

If it matters, I have an alias defined for this program:

alias optipng='optipng -o7 -zm1-9 -strip all -fix -preserve'

And I want that to be applied. Please advise. The simpler the better. If POSIX, then best.

I admit I never have used xargs or parallel, and I assume they could be used.

Found this code in here, but it is not explained, therefore if applicable, please elaborate.

1 Answers1

2

The simplest method to process your N files, where N is approximately 500, is likely to create a script optipng.in that is N lines long, and contains one line to process each file:

#!/bin/bash
for file in *.png; do
    printf 'optipng -o7 -zm1-9 -strip all -fix -preserve %q\n' "$file"
done > optipng.in

Inspect your optipng.in file to ensure that it is correct, and issues the correct command(s) against each of the N files you want to process. Once you have verified that it looks good, run those N commands via parallel:

parallel < optipng.in

For a run of that size, it may be wise to capture the standard output and standard error, so you can inspect which files were processed normally and which might have triggered errors:

parallel < optipng.in > optipng.out 2>&1

Further refinements could involve separating the stdin/stdout redirection for each invocation of optipng, etc. etc.

Jim L.
  • 7,997
  • 1
  • 13
  • 27