Pass all elements in existing array to xargs Pass all elements in existing array to xargs unix unix

Pass all elements in existing array to xargs


When you do

echo ${FILES[@]} | xargs -i mv '{}' /path/to/destination

xargs treats the entire line as a singe argument. You should split each element of the array to a new line, and then xargs should work as expected:

printf "%s\n" "${FILES[@]}" | xargs -i mv '{}' /path/to/destination

Or if your filenames can contain newlines, you can do

printf "%s\0" "${FILES[@]}" | xargs -0 -i mv '{}' /path/to/destination