2

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?

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
clappboard
  • 35
  • 1
  • 5

2 Answers2

6

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.

thapakazi
  • 341
  • 3
  • 11
1

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 {} \;
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • Thanks, that helps a lot! The problem is that I need everything compiled into one *.sh file so that it is portable and easily distributed... Is there any way to call a function in a BASh script? – clappboard Jul 15 '12 at 18:25
  • Sure. Shell scripts execute commands, aliases, and functions, although some *commands* are more limited. See http://www.gnu.org/software/bash/manual/html_node/Shell-Functions.html for specifics about Bash functions. – Todd A. Jacobs Jul 15 '12 at 18:48