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
-I
implies -x and -L 1. Is something likecp $(echo 'file1' 'file2' 'file3') somedir
an option for you?cp
andmv
commands, this is the reason for the-t
option, which allows you to put the destination directory at the front.args can be arbitrary, they are passed as a single string separated by spaces
, you might usecp ${single_string} /somedir
. When filenames with special characters are already embedded in single quotes, this should work.