Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

ie

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
add comment

3 Answers 3

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
add comment

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}; \
 done
share|improve this answer
    
I've very good results with it, thanks –  mozerella Apr 10 '13 at 17:26
add comment

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
add comment

Your Answer

 
discard

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.