17

Suppose I have a dir tree like this:

ROOTDIR
    └--SUBDIR1
        └----SUBDIR2
            └----SUBDIR3

I am looking for a command such that when I input:

$ [unknown command] ROOTDIR

The whole dir tree can be deleted if there is no file but only dirs inside the whole tree. However, say if there is a file called hello.pdf under SUBDIR1:

ROOTDIR
    └--SUBDIR1
        └--hello.pdf
        └----SUBDIR2
            └----SUBDIR3

Then the command must only delete SUBDIR2 and below.

gsklee
  • 385

6 Answers6

33
find ROOTDIR -type d -empty -delete

same as

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

but uses the built in "-delete" action.

Note that "-delete" implies "-depth".

go2null
  • 331
  • Kudos for the most succinct answer by using find's own built-in delete! I am adding this to my local utilscripts! – jamadagni Sep 19 '14 at 02:26
  • Simplest, and the explanation about -delete implying -depth removes any lingering doubt. Thanks! – Álex Feb 11 '21 at 12:25
11

Alexis is close. What you need to do is this:

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

That will first drill down the directory tree until it finds the first empty directory, then delete it. Thus making the parent directory empty which will then be deleted, etc. This will produce the desired effect (I do this probably 10 times a week, so I'm pretty sure it's right). :-)

  • Why is -depth option necessary? find . -type d -empty -exec rmdir "{}" \; should also work.... right? – Abhishek A Nov 08 '11 at 14:26
  • 4
    Consider if you have a tree (directories only) foo/bar/baz. Unless you use -depth, it will try to delete foo first, fail, and you'll end up with foo/bar after running. – l0b0 Nov 08 '11 at 14:43
  • 1
    Possibly alternative is to use + instead of ; so you batch remove directories. Since you're doing it depth-first the children will still be removed before the parents (possibly dependent on your version of rmdir/bash and reliant on rmdir not deleting nonempty directories). This works for me in bash on cygwin: mkdir -p a/b/c/d ; find a -depth -type d -exec rmdir {} + – idbrii Dec 19 '13 at 23:05
  • 4
    People, go for go2null's much more succinct answer below! Can't understand why SE gives priority to accepted answers rather than answers with most upvotes in displaying the answers below the question. The OP accepts the best answer available at his time of choosing, but later on much better answers can come which the community upvotes, no? (Of course, this is something for meta...) – jamadagni Sep 19 '14 at 02:30
  • this does not work for me. it only deletes the deepmost leaf (SUBDIR3 in this case) – Joey Baruch Oct 26 '17 at 11:01
3

I would try this:

find ROOTDIR -type d -depth -exec rmdir {} \;
Alexis
  • 166
1

Here are some requirements before we can do it safely:

  1. remove subdirectories first and then upper level directories, i.e. we need to sort directory listing or use rmdir --parents flag
  2. start ROOTDIR always with / or ./ to avoid surprises with files beginning with -
  3. use NUL terminated list of directories to work with directory names with spaces

Here is how I'd do that in shell:

find ./ROOTDIR -type d | sort -r | tr '\n' '\000' | xargs -0 rmdir --ignore-fail-on-non-empty

If you don't mind some redundant errors then you can just force removing all directories with parents and you don't need to do any sorting (you can't sort NUL terminated strings which adds need to tr)

find ./ROOTDIR -type d -print0 | xargs -0 rmdir --ignore-fail-on-non-empty --parents
Cougar
  • 111
  • Kudos for the detailed explanation of your answer. I'd probably have used the same approach, until I learned about -empty -delete options to find in @go2null's answer. – Davor Cubranic Jul 08 '19 at 17:10
0
rmdir $(find ROOTDIR -type d | sort -r)
lanzz
  • 125
  • 2
  • 6
    This won't work if any of the directory names contains whitespace or globbing characters. It's generally a bad idea to use command substitution on a list of file names. It's especially a bad idea with find because find has a way to do the processing cleanly: find … -exec. – Gilles 'SO- stop being evil' Nov 07 '11 at 23:01
  • 1
    Thanks to Gilles for pointing that out. @lanzz, usually posting just a command without explaining what it does (and in this case, the pitfalls) is not enough. Please add to your answer. – nopcorn Nov 08 '11 at 03:06
0

I would do this:

find ROOTDIR -type d | xargs -0 -I {} rmdir {}
Michael Mrozek
  • 93,103
  • 40
  • 240
  • 233
chemila
  • 101