May 19
Using find and xargs on directories with spaces
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 :)
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
Cool, that’s even more straightforward. Thanks for the tip :)
Thanks, toledo! That’s exactly what I needed and very straight-forward.