Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 3 down vote accepted

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).

share|improve this answer
if -delete doesn't work, use "find . -type d -empty -exec rmdir {} \;" (don't forget the space behind {}, it's important). – FSMaxB Feb 5 '13 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. – mr.spuratic Feb 5 '13 at 20:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.