Unix Power Tools
by Jerry Peek
xargs: Problems with Spaces and Newlines
xargs command reads its input and splits the
arguments at spaces or newlines. It's legal (though pretty unusual)
for UNIX filenames to have spaces or newline characters in them.
Those filenames can cause
For example, I have a directory full of copies of Usenet articles. The filenames are the same as the subjects of the articles:
The problem comes when I run a command like this:
find outputs the pathname
Security Holes, the
xargs command would most
rm to remove four filenames:
Holes. I'd probably get four error messages from
rm because no files with those names exist. If they
did exist, though, they'd be removed when they shouldn't!
Newlines in filenames can cause the same problems.
Some versions of
xargs (see below) are better at
handling this problem. Here's a simple test to see how well your
system's version works.
Make an empty directory, a filename with spaces, and a filename with a newline. Try to remove the file:
xargs broke the filenames at the space and newline.
If it hadn't broken the filenames, the files would have been removed.
xargs (used on Linux) has a
(zero) option; this means the pathnames it reads are separated by NUL
characters instead of whitespace. GNU's
find (also used
by Linux) has a
-print0 operator that puts a NUL
between pathnames instead of a newline. Use them together like
Because UNIX pathnames won't contain NULs, this combination should never fail. (Try it!)