38

I have a lot of images that I would like to process with pngquant. They are organized in a pretty deep directory structure, so it is very time-consuming to manually cd into every directory and run pngquant -ext .png -force 256 *.png

Is there a way to get this command to run on every *.png in every directory within the current one, as many layers deep as necessary?

1
  • 3
    What operating system are you on? Commented Mar 10, 2012 at 19:30

2 Answers 2

77

If you have limited depth of directories and not too many files, then lazy solution:

pngquant *.png */*.png */*/*.png

A standard solution:

find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;

and multi-core version:

find . -name '*.png' -print0 | xargs -0 -P8 -L1 pngquant --ext .png --force 256

where -P8 defines number of CPUs, and -L1 defines a number of images to process in one pngquant call (I use -L4 for folders with a lot of small images to save on process start).

Sign up to request clarification or add additional context in comments.

7 Comments

Alternatively, depending on whether you have a reasonably recent bash or zsh, and the option is enabled (shopt -s globstar in bash), you can use a recursive glob: pngquant **/*.png
find . -name '*.png' -exec pngquant --ext .png --force 256 {} \; fails with File not found - '*.png'
Sometimes the XARGS version doesn't work. It prints 'Bad file number'. I think problem in paths
@porneL: The multi-core version wasn't actually multi-core, as without -L1 it adds all arguments to a single pngquant call, which processes them serially :). Fixed.
What could be a Windows equivalent of the multi-core command?
|
24

With the fish shell you can run the following from the root of your project directory

pngquant **.png

Which will generate new files with extensions like -or8.png or -fs8.png.

If you want to overwrite the existing files, you can use

pngquant **.png --ext .png --force

1 Comment

Also on bash 4.0 afterwards with shopt -s globstar set, or zsh by default, etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.