You didn't mention what xargs
are you using.
If you use BSD xargs
instead of GNU xargs
, there is a powerful option -L
that can do what you want:
-J replstr
If this option is specified, xargs will use the data read from
standard input to replace the first occurrence of replstr instead
of appending that data after all other arguments. This option
will not affect how many arguments will be read from input (-n),
or the size of the command(s) xargs will generate (-s). The op-
tion just moves where those arguments will be placed in the com-
mand(s) that are executed. The replstr must show up as a dis-
tinct argument to xargs. It will not be recognized if, for in-
stance, it is in the middle of a quoted string. Furthermore,
only the first occurrence of the replstr will be replaced. For
example, the following command will copy the list of files and
directories which start with an uppercase letter in the current
directory to destdir:
/bin/ls -1d [A-Z]* | xargs -J % cp -Rp % destdir
Examples
$ seq 3 | xargs -J@ echo @ fourth fifth
1 2 3 fourth fifth
seq 3 | xargs -J@ ruby -e 'pp ARGV' @ fourth fifth
["1", "2", "3", "fourth", "fifth"]
The Ruby code pp ARGV
here would pretty print the command arguments it receives. I usually use this way to debug xargs
scripts.