1

I have a directory containing lots of files and lots of empty directories. I want to keep the files but remove the directories. How can I achieve this using rm and other standard unix tools?

1 Answer 1

3

Something along the lines:

find . -type d -empty -delete

I believe the -delete is a GNUism, in which case you have to do:

find . -type d -empty -print0 | xargs -0 rm

This handles the case of file names with "strange" characters (but it seems -print0 and -0 are again GNU extensions).

2
  • 1
    if -delete doesn't work, use "find . -type d -empty -exec rmdir {} \;" (don't forget the space behind {}, it's important).
    – FSMaxB
    Feb 5, 2013 at 19:07
  • You may need an explicit -depth with find, depending on the invocation, so that deeper directories are removed first. -delete automatically uses -depth, the "find ... -print0 | xargs" version does not, and would leave file-less directory hierarchies undeleted. Feb 5, 2013 at 20:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .