Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using find to pass all directories to xargs, and the command on each dir by xargs needs to create a new directory for each, looking like dirname_a, so how can this be done?


find . -type d -print0 | xargs -0 mkdir ${DIR}_a && mogrify -path ${DIR}_a -resize '1200x1200>' -quality 95 ${DIR}/*{.jpg|.JPG} {} \;

${DIR} is not working as it must be wrong

I can use

for D in ./* ; do mkdir ${D}_a && mogrify -path ${D}_a -resize '1200x1200>' -quality 95 ${D}/*{.jpg|.JPG}; done

but this won't run on either directories or files with spaces in the names, so that is why I was looking at find with xargs.

share|improve this question
As a general rule you will get better answers if you actually explain what you are trying to do. Not everyone is familiar enough with imagemagick and shell tools to figure out what you are attempting. Even for those of us who are, it is not easy to decipher what you have posted. – terdon Apr 10 '13 at 17:19
up vote 1 down vote accepted

Here's a nice xargs trick with the -I and -n1 parameter combination.

find . -mindepth 1 -type d | xargs -n1 -I {} bash -c 'mkdir "{}_a" && mogrify -path "{}_a" -resize 1200x1200\> -quality 95 "{}/"*.{jpg,JPG}'

-I {} tells xargs to do a replacement when it finds the {} string in the command. -n1 tells xargs to process the find output one line by one.

You want to spawn multiple instances of mogrify, you can add the -P5 parameter to xargs.

This will spawn 5 process at a time, and reduce considerably your process time especially if you have a lot of pictures to convert.

share|improve this answer
now that's impressive. and you used xargs which was what my question was about. good work. – mozerella Apr 17 '13 at 22:51

Don't use xargs. Try a loop instead:

find . -mindepth 1 -type d | 
 while IFS= read -r dir; do mkdir "$dir"_a && 
  mogrify -path "$dir"_a -resize '1200x1200>' -quality 95 "$dir/"*.{jpg,JPG}; \
share|improve this answer
I've very good results with it, thanks – mozerella Apr 10 '13 at 17:26

If the string stored in a variable can contain spaces, you have to put double quotes around the variable, that is,

for D in ./* ; do mkdir "$D"_a && .... ; done
share|improve this answer
This works well too now, with quotes, thanks Uwe – mozerella Apr 10 '13 at 18:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.