LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to Linux Subscribe to Newsletters

Unix Power Tools
xargs: Problems with Spaces and Newlines

by Jerry Peek
02/03/2000

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

For example, I have a directory full of copies of Usenet 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:


% 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 (used on Linux) has a -0 (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 this:


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

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


Back More Unix Power Tools

 




Tagged Articles

Be the first to post this article to del.icio.us

Recommended for You

  1. Cover of Building Secure Servers with Linux
    Building Secure Servers with Linux
    Print: $44.95
  2. Cover of VMware Cookbook
    VMware Cookbook
    Print: $39.99
    Ebook: $31.99
  3. Cover of sed and awk Pocket Reference
    sed and awk Pocket Reference
    Print: $9.95
    Ebook: $7.99
  4. Cover of Apache: The Definitive Guide
    Apache: The Definitive Guide
    Print: $34.95

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2009, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com