Had some fun with xargs this morning. I came up with such a monstrosity, I have to record it for posterity.
The situation was one where we had a script that was scping a whole pile of files around, and the source directory got so big, that a straight "scp ${SOURCE}/* $DESTINATION" was resulting in the good old command-line too long situation.
Sounds like a job for xargs I say. But how do we tack the destination on the end? I'm used to situations where you just want to pass a whole lot of arguments to a command, but not have a constant value on the end. Oh, and this was on Solaris for good measure, so I was fully expecting to not be able to do it.
Did some prototyping. Put "a-z" in /tmp/alphabet, one per line.
apollock@caesar:~$ cat /tmp/alphabet | xargs -L 6 echo a b c d e f g h i j k l m n o p q r s t u v w x y z
Right, so that solved the argument length issues, but I needed a constant argument on the end. I tried
apollock@caesar:~$ cat /tmp/alphabet | xargs -L 6 -i echo '{}' foo a foo b foo c foo d foo e foo . . .
But as you can see, (and so the manpage says), -i implies -l1 (which is the same as -L 1). Bummer.
So then I came up with this ripper:
apollock@caesar:~$ cat /tmp/alphabet | xargs -L 6 echo | xargs -i echo '{}' foo a b c d e f foo g h i j k l foo m n o p q r foo s t u v w x foo y z foo
That's the ticket!