Bash – How to get the current line count from xargs

bashshellxargs

I want to grab every 5 images in a directory and merge them together into a new file with a unique filename per 5 image group.

find *.jpg | xargs -n 5 -i convert {} -append {#}.png

This is really close but the {#} doesn't output the current line count. Is this even possible with xargs?

Best Answer

If the filenames does not contain whitespaces:

Dry run:

find *.jpg | xargs -n 5 | awk '{OFS=" ";}{print "convert",$1,$2,$3,$4,$5,"-append",NR".png\n";}'

If everything looks okay, append | sh.

Related Question