0

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.

1
  • 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, 2013 at 17:19

3 Answers 3

2

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.

1
  • now that's impressive. and you used xargs which was what my question was about. good work.
    – mozerella
    Apr 17, 2013 at 22:51
1

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
0
1

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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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