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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.