I was a bit shocked (way back) when I discovered that xargs doesn't handle spaces in filenames. /bin/find writes out filenames one per line (separates filenames by newlines), so I had long assumed that xargs read arguments one-per-line and spaces wouldn't be a problem. But xargs splits the lines that it reads in on whitespace.
The standard solution (in a few tools) is a rather ugly 'find -print0' and 'xargs -0'. The fix is nice in that it is a general solution to the rather stupid fact that you can even put newlines into file names on most Unix filesystems. It is particularly ugly in that there are tons of things that don't support the idiom of "read from a pipe a list of '\0'-separated strings" and/or don't support the idiom of "write to a pipe a list of '\0'-separated strings". For example, "find ... -print0 | grep ... | sed ... | xargs -0 ..." isn't likely to work properly.
I repeatedly run into cases where somebody thought it was reasonable to include a space in the name of a file. I have never run into a case where somebody thought it was reasonable to include a newline in a filename. So I want xargs to handle spaces in filenames without having to stop using all of my tools that know how to deal with filtering lines of text. So I wrote yargs. It is terribly complicated:
#!/bin/sh perl -pe's/ /\\ /g' | xargs "$@"
- tye
In reply to yargs -- xargs but handle spaces in filenames by tye
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |