Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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
find . * -depth -print0 | xargs -0 rmdir

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

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

I once nuked my OS by running some command off the internet (which I didnt understand) like that

share|improve this question
In case this helps answer the question: I'm running Lubuntu 14.04/10 amd64 with Linux 3.16-rc3 – Natus Vincere Jul 7 '14 at 10:31
What if / is having some empty folders and later it will be used by some applications.It's not safe to run that command from / – Thushi Jul 7 '14 at 10:42
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 '14 at 10:47
Or find . -depth -exec rmdir {} + eliminates calling xargs and deletes multiple empty directories at once. – Christopher Jul 7 '14 at 11:04
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 '14 at 11:37
up vote 5 down vote accepted

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.

share|improve this answer
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. – Natus Vincere Jul 7 '14 at 10:55
@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 '14 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...

share|improve this answer

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

share|improve this answer
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. – Natus Vincere Jul 7 '14 at 10:50
rmdir will only ever remove empty directories, but that doesn't mean it's safe. Some empty directories need to exist. – Gilles Jul 7 '14 at 21:55

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.