May 19

Using find and xargs on directories with spaces

Posted by mtoledo
Filed under linux | 3 Comments

Quick tip:

I recently moved my music library from the windows partition into the Linux one.

One of the things I had to do was remove those pesky ‘desktop.ini’ files windows creates for the cd thumbails.

Ends up using xargs as I wanted at the beginning didn’t work correctly because of the spaces in the directory names:


find . -name desktop.ini  | xargs  rm

This won’t work because it will use spaces as separators. The correct way of doing it is:


find . -name desktop.ini -print0 | xargs -0 rm

where “print0″ and “-0″ tells find and xargs to use the NUL ASCII character rather than spaces to separate arguments.

Thanks to Not So Frequently Asked Questions, where I found the solution :)

This entry was posted on Tuesday, May 19th, 2009 at 1:31 am and is filed under linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “Using find and xargs on directories with spaces”

  1. coderrr on May 19th, 2009 at 9:30 am

    Hey, there’s also
    find . -name … -exec rm {} \;
    which rm’s one file at a time
    or
    find . -name … -exec rm {} +
    which rm’s in bulk

  2. mtoledo on May 19th, 2009 at 10:08 am

    Cool, that’s even more straightforward. Thanks for the tip :)

  3. dionak on October 13th, 2009 at 10:41 pm

    Thanks, toledo! That’s exactly what I needed and very straight-forward.

Leave a Reply