home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 9.21 Handle Too-Long Command Lines with xargs Chapter 9
Saving Time on the Command Line
Next: 9.23 Workaround for "Arguments too long" Error
 

9.22 xargs: Problems with Spaces and Newlines

The xargs ( 9.21 ) 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 xargs trouble.

For example, I have a directory full of copies of Usenet ( 1.33 ) articles. The filenames are the same as the subjects of the articles:


% 

ls


A use for the "yes" command
Beware UNIX Security Holes
Causes of 'test' errors
   ...

The problem comes when I run a command like this:

% 

find . -type f -mtime +7 -print | xargs rm

If find outputs the pathname ./Beware UNIX Security Holes , the xargs command would most likely tell rm to remove four filenames: ./Beware , UNIX , Security , and 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:




touch
 

\
 






% 

mkdir temp


% 

cd temp


% 

touch 'Just testing'


% 

touch 'some\




file'


% 

find . -print | xargs rm


./Just: No such file or directory
testing: No such file or directory
some: No such file or directory
file: No such file or directory

That xargs broke the filenames at the space and newline. If it hadn't broken the filenames, the files would have been removed.

The GNU xargs (on the CD-ROM) has a -0 (zero) option; this means the pathnames it reads are separated by NUL characters instead of whitespace. GNU's find (also on this disc) has a -print0 operator that puts a NUL between pathnames instead of a newline. Use them together like this:

% 

find . -type f -mtime +7 -print0 | xargs -0 rm

Because UNIX pathnames won't contain NULs, this combination should never fail. (Try it!)

- JP


Previous: 9.21 Handle Too-Long Command Lines with xargs UNIX Power Tools Next: 9.23 Workaround for "Arguments too long" Error
9.21 Handle Too-Long Command Lines with xargs Book Index 9.23 Workaround for "Arguments too long" Error

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System