217

How can I remove all empty directories in a subtree? I used something like

find . -type d -exec rmdir {} 2>/dev/null \;

but I needs to be run multiple times in order to remove directories containing empty directories only. Moreover, it's quite slow, especially under cygwin.

maaartinus
  • 5,059

8 Answers8

327

Combining GNU find options and predicates, this command should do the job:

find . -type d -empty -delete
  • -type d restricts to directories
  • -empty restricts to empty ones
  • -delete removes each directory

The tree is walked from the leaves without the need to specify -depth as it is implied by -delete.

60

List the directories deeply-nested-first.

find . -depth -type d -exec rmdir {} \; 2>/dev/null

(Note that the redirection applies to the find command as a whole, not just to rmdir. Redirecting only for rmdir would cause a significant slowdown as you'd need to invoke an intermediate shell.)

You can avoid running rmdir on non-empty directories by passing the -empty predicate to find. GNU find tests the directory when it's about to run the command, so directories that have just been emptied will be picked up.

find . -depth -type d -empty -exec rmdir {} \;

Another way to speed up would be to group the rmdir invocations. Both are likely to be noticeably faster than the original, especially under Cygwin. I don't expect much difference between these two.

find . -depth -type d -print0 | xargs -0 rmdir 2>/dev/null
find . -depth -type d -exec rmdir {} + 2>/dev/null

Which method is faster depends on how many non-empty directories you have. You can't combine -empty with methods for grouping invocations, because then the directories that only contain empty directories aren't empty by the time find looks at them.

Another method would be to run multiple passes. Whether this is faster depends on a lot of things, including whether the whole directory hierarchy can remain in the disk cache between find runs.

while [ -n "$(find . -depth -type d -empty -print -exec rmdir {} +)" ]; do :; done

Alternatively, use zsh. The glob qualifier F matches non-empty directories, so /^F matches empty directories. Directories that only contain empty directories can't be matched so easily.

while rmdir **/*(/N^F); do :; done

(This terminates when rmdir receives an empty command line.)

  • That's it. Instead of 90 seconds it takes 0.90 s. – maaartinus Mar 01 '11 at 22:14
  • @maaartinus: I'm curious: do you have a similar data set where you could try without -p? I wouldn't have thought it would make a difference. – Gilles 'SO- stop being evil' Mar 01 '11 at 22:15
  • 3
    @maartinus — other little optimizations: adding -empty should work with this one (although I'm not sure exactly how much it'll gain). And very, very trivially, since you probably don't want to remove ., use -mindepth 1. – mattdm Mar 01 '11 at 22:19
  • It was not the removal but the process start up overhead, what took nearly all the time. I had overlooked the -depth argument, which makes rmdir -p useless. I've changed my comment already. The 90 s was my original attempt; there's nothing surprising here. – maaartinus Mar 01 '11 at 22:20
  • @mattdm: -empty will not work: if you have a/b with b empty and the only entry in a, find would not consider a empty. And I expect -mindepth 1 to be slower (probably not measurably so) because it adds an extra test for each file just to very rarely skip calling rmdir on .. – Gilles 'SO- stop being evil' Mar 01 '11 at 22:25
  • @Gilles: -empty works with rmdir -p; not otherwise. And although I haven't tested, -mindepth 1 is an option rather than a test, and therefore should only happen once. At least, I think. (That also may be a GNUism.) – mattdm Mar 01 '11 at 22:34
  • @mattdm: oh, right, -empty does work with rmdir -p. As to whether it helps, I think not, but it's not clear-cut: -empty + -p means fewer calls to rmdir(1) (for large data sets) but more calls to rmdir(2) and stat(2). – Gilles 'SO- stop being evil' Mar 01 '11 at 22:45
  • 2
    I realized we can remove the rmdir command call altogether, at least with GNU find, with this command: find . -depth -type d -empty -delete – Christophe Drevet Jan 02 '14 at 14:26
7

find . -depth -type d -exec rmdir {} +

is the simplest and standard compliant answer to this question.

The other answers given here unfortunately all depend on vendor specific enhancements that do not exist on all systems.

schily
  • 19,173
6

If you just tack a -p on your rmdir, that'll work in one pass. It won't be pretty or optimal, but it should get everything. That tells rmdir to remove any non-empty parent directories of the one you're removing.

You can save a little bit by adding the -empty test to find, so it doesn't bother with non-empty directories.

mattdm
  • 40,245
1

I use these aliases for frequently used find commands, especially when I cleaning up disk space using dupeguru, where removing duplicates can result in a lot of empty directories.

Comments inside .bashrc so I won't forget them later when I need to tweak it.

# find empty directories
alias find-empty='find . -type d -empty'

# fine empty/zero sized files
alias find-zero='find . -type f -empty'

# delete all empty directories!
alias find-empty-delete='find-empty -delete'

# delete empty directories when `-delete` option is not available.
# output null character (instead of newline) as separator. used together
# with `xargs -0`, will handle filenames with spaces and special chars.
alias find-empty-delete2='find-empty -print0 | xargs -0 rmdir -p'

# alternative version using `-exec` with `+`, similar to xargs.
# {}: path of current file
# +: {} is replaced with as many pathnames as possible for each invocation.
alias find-empty-delete3='find-empty -exec rmdir -p {} +'

# for removing zero sized files, we can't de-dupe them automatically
# since they are technically all the same, so they are typically left
# beind. this removes them if needed.
alias  find-zero-delete='find-zero -delete'
alias find-zero-delete2='find-zero -print0 | xargs -0 rm'
alias find-zero-delete3='find-zero -exec rm {} +'
raychi
  • 1,191
  • 1
  • 8
  • 4
0

find . -type d -printf "%d %p\n" |\ sort -nr |\ perl -pe 's/^\d+\s//;' |\ while read dir; do \ (rmdir "$dir" > /dev/null 2>&1); \ done

Here's how it works:

  1. Recursively list all the directories along with their depth
  2. Sort by descending order of their depth
  3. Filter out only the directory paths
  4. Run rmdir on the list one by one
0

Found a simple but effective solution which should be also POSIX compliant:

while find /path/to -type d -empty -print -exec rmdir {} + | grep -q ^; do :; done

It's looping a find command which executes rmdir -v in every round. If no more empty dirs are found, no output is generated, grep returns negative and breaks the loop.

One solution of https://unix.stackexchange.com/a/8433/496909 is quite similar. It's utilizing a subshell and this a grep. Don't know which one is better.

PS: If you're interested in the removed directories just remove -q.

-2

rm -r */ command worked easily for me. rm should require -f to forcefully remove directories with files. rm -r should only remove empty directories. I'm open to why this could be wrong. This should also leave files since */ only looks at folders.

  • 1
    I'd strongly recommend to test it thoroughly first as rm is primarily meant to remove files. While */ only matches directories, I have no idea what it does on deeper levels. I can also imagine that it works on some systems only. – maaartinus Jun 22 '18 at 19:30
  • 1
    Slippery slope in its finest. – Martin Braun Mar 18 '23 at 02:36