clappboard
clappboard

Reputation: 45

How can I convert JPG images recursively using find?

Essentially what I want to do is search the working directory recursively, then use the paths given to resize the images. For example, find all *.jpg files, resize them to 300x300 and rename to whatever.jpg.

Should I be doing something along the lines of $(find | grep *.jpg) to get the paths? When I do that, the output is directories not enclosed in quotation marks, meaning that I would have to insert them before it would be useful, right?

Upvotes: 3

Views: 3642

Answers (2)

thapakazi
thapakazi

Reputation: 361

I use mogrify with find.

Lets say, I need everything inside my nested folder/another/folder/*.jpg to be in *.png

find . -name "*.jpg" -print0|xargs -I{} -0 mogrify -format png {}

&& with a bit of explaination:

find . -name *.jpeg -- to find all the jpeg's inside the nested folders.
-print0 -- to print desired filename withouth andy nasty surprises (eg: filenames space seperated)
xargs -I {} -0 -- to process file one by one with mogrify

and lastly those {} are just dummy file name for result from find.

Upvotes: 7

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84343

You can use something like this with GNU find:

find . -iname \*jpg -exec /your/image/conversion/script.sh {} +

This will be safer in terms of quoting, and spawn fewer processes. As long as your script can handle the length of the argument list, this solution should be the most efficient option.

If you need to handle really long file lists, you may have to pay the price and spawn more processes. You can modify find to handle each file separately. For example:

find . -iname \*jpg -exec /your/image/conversion/script.sh {} \;

Upvotes: 1

Related Questions