find . * -depth -print0 | xargs -0 rmdir

It finds and removes all empty folders (including hidden ones) recursively.

I only tried it on my home folder and a pendrive in Linux PC and it worked but I don't know if it is safe to run from / as root

I once nuked my OS by running some command off the internet (which I didn't understand) like that.

  • 3
    The . * is pointless, that's not how find works. That means "find everything in . or all files and folders (*)". The result is that everything is found twice. What you wanted was find . alone or find . -name '*' which is the same thing.
    – terdon
    Jul 7, 2014 at 10:47
  • 4
    Read man find and look for -exec, that will explain the +. The error is because @Christopher did not specify -mindepth 1 so it is also finding your current directory (.) and attempting to delete it.
    – terdon
    Jul 7, 2014 at 11:37
  • 1
    1) you should really add also -type d so you don't run thousands of "rmdir" on files... 2) may we ask you why you want to remove empty directories everywhere? As some already said : it could be problematic (programs needing a directory, creating it when installed, but having it deleted before they could use it could result in a crash, or unexpected behavior) Jul 8, 2014 at 8:59
  • 1
    An empty directory isn't unnecessary. It's usually there for a reason. Just because it's empty when you look doesn't mean there won't be a file there later. An empty directory also has zero impact on performance. It isn't causing any harm. Never remove a file just because you don't understand why it's there. Jul 8, 2014 at 10:28
  • 1
    Nothing is safe without understanding. Unless it affects nothing. In which case it is safe, but purposeless.
    – Wildcard
    Sep 15, 2016 at 23:38

3 Answers 3


The man page for rmdir says:-

Remove the DIRECTORY(ies), if they are empty.

If you want to remove all empty directories then it will be safe. The question you need to ask is:-

Do you want to remove all empty directories?

Some applications need a directory even if it's empty. For example, journald can be configured so that it only logs to persistent storage if /var/log/journald exists. If you run your command when that directory is empty then it will be deleted. Afterwards journald will not log to persistent storage as it can't find the directory. I believe Fedora is configured this way by default.

Also, empty (unmounted) mount points could also be deleted by your command. They should be reasonably easy to fix, but it could still catch you out.

  • Thanks this is exactly what I'm concerned about.I know that some applications use empty directories but I'm asking just in case someone might know to make sure that it is safe for my current setup. Jul 7, 2014 at 10:55
  • 1
    @NatusVincere we can't know unless you tell us what your current setup is. As garethTheRed explained it can sometimes be dangerous. More to the point, it is almost certainly unneeded. Just leave the / directory alone.
    – terdon
    Jul 7, 2014 at 11:38

Though this command may be safe in most cases, it is a bad habit to use * where the command can accept options, because a filename starting with a dash (created by mistake or by a malicious person who could have exploited another bug) could be interpreted as an option. As someone said, the * is pointless here, but what you need to remember is that, especially when running a command as root, you need to control all the arguments. In general, avoid globbing without a prefix (for instance, instead of *, ./* is often better). Also, be careful with symbolic links and spaces in filenames...


find will print all files and folders in the directory tree. xargs runs the command supplied - rmdir for each element find returns.

rmdir will not remove folders that aren't empty, and will not remove files either - you can try it. mkdir a folder, touch a few files inside it and then run rmdir on the folder. rmdir will complain with something like this: rmdir: failed to remove ‘test’: Directory not empty, or rmdir: failed to remove ‘test.txt’: Not a directory

So yes, it should be perfectly safe. If you want to test, you can create a set of test folders using mkdir and touch and see if it does what you expect it to.

Also, for more info on rmdir - and nearly any command in *nix, run man <command>.

  • Thanks for the command explanation, I find it useful.I've already tried this in my home directory and flash storage media (as I wrote in the question) to find out how safe it is but I realized that some applications might require some empty directories. Jul 7, 2014 at 10:50
  • 1
    rmdir will only ever remove empty directories, but that doesn't mean it's safe. Some empty directories need to exist. Jul 7, 2014 at 21:55

You must log in to answer this question.

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