Pages

07 July, 2024

Pass multiple arguments to single command in custom position xargs

I want execute a command where I have the args as single string but they need to be in a custom position. E.g. in the following example I want the args to be positioned before any fixed args that I have written after the cp comamnd i.e. the path argument /somedir


I want:
echo -n 'file1 file2 file3' | xargs -d ' ' -I{} cp {} /somedir



to behave like:
cp file1 file2 file3 /somedir



but it doesn't...


It also looks like when including the -I{} option, the -d option makes xargs behave differently. It seems like it tries to pass each argument to a separate command call rather than all of them toa single one.


For example, this works:
echo -n 'file1 file2' | xargs -d' ' diff



But this fails
echo -n 'file1 file2' | xargs -d' ' -I{} diff {}



With error:
diff: missing operand after 'file1'
diff: Try 'diff --help' for more information.
diff: missing operand after 'file2'
diff: Try 'diff --help' for more information.



How can I get echo -n 'file1 file2 file3' | xargs -d ' ' -I{} cp {} /somedir to behave as intended

No comments:

Post a Comment

Thanks