I would like to suggest an addition to the standard to address the
common problem of how to handle special characters in filenames
when using find and xargs together.
Currently it is not possible to use find and xargs together in a way
that is both portable and will work for all possible filenames. The
best you can do, using only standard commands, is to add suitable
quoting to the pathnames in between find and xargs, e.g.:
find . -type f | sed -e "s/'/'\\\\''/g" -e "s/.*/'&'/" | xargs ...
However, this doesn't work for pathnames containing newline characters,
and it is difficult to get the quoting exactly right. (The sed command
above puts single quotes around each line, after changing any single
quote characters to '\'', i.e. a closing quote followed by a
backslash-quoted quote then a new opening quote.)
I know of two existing solutions to the problem. One is the GNU
extension:
find . -type f -print0 | xargs -0 some_command
which causes find to output null-terminated pathnames instead of
newline-terminated ones, and causes xargs to read in null-terminated
pathnames and to turn off its special handling of backslash and quotes.
The other existing solution is an extension to "find" to make it do
the argument aggregation internally, so xargs is not needed:
find . -type f -exec some_command {} +
Here the use of "+" instead of ";" as the command terminator causes
find to substitute sets of pathnames for "{}" instead of single
pathnames. I believe this works on all SVR4-derived systems.
These solutions both work fine - the only problem is that they are
not portable. I would like to see one of them added to the standard.
Of course, to do so we would have to file a POSIX.2 interp request
in order to bring the change into scope.
--
Geoff Clare yyy@xxxxxxxxxxx
UniSoft Limited, London, England. yyy@xxxxxxxxxx
|