Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

This should be fairly simple, but I am not sure what i'm missing.

I'd like to recursively delete empty directories, and get an output for what's being deleted, this command works for sure, but I just can't print the actions of -excec verbosely.

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

by recursively I mean, I want to keep deleting empty folders, until there's no other empty folder.

$ tree .
.
├── empty
│   └── empty
│       └── empty
└── non-emty
    └── ko

this item will only remove one empty folder in the tree

$ find . -depth -type d -empty -exec rmdir -v {} +
rmdir: removing directory, `./empty/empty'
share|improve this question
1  
I worry about the logic of this -- is a directory empty if it only contains directories that are empty? Also -- as far as logging the output -- whenever I do something like this that has the potential to wreck a whole bunch of stuff, I have it output the command it would run but not actually run the command itself. Then, if I actually want to run those commands I can just run the output like a shell script. –  chris Apr 14 '13 at 13:09
    
you right, i have the wrong verbiage, it's not quite empty. let me change that. it has nothing to do with logic, lol there's a problem, keep deleting empty directories until there are none left. I try to solve. –  kmassada Apr 14 '13 at 13:13
add comment

1 Answer 1

up vote 6 down vote accepted

You don't need the while loop and you should use rmdir -p to remove the empty parents

find . -depth -type d -empty -exec rmdir -v -p {} + 
rmdir: removing directory, `./1/3'
rmdir: removing directory, `./1'
rmdir: removing directory, `.'
rmdir: failed to remove directory `.': Invalid argument
rmdir: removing directory, `./2/3'
rmdir: removing directory, `./2'
rmdir: failed to remove directory `./2': Directory not empty

The reason you don't see the output with your command is that you are running it in a subshell $(...) but doing nothing with the returned output you could put an echo before the subsell to print out what it returns

echo $(find . -depth -type d -empty -exec rmdir -v -p {} +)
rmdir: removing directory, `./1/3' rmdir: removing directory, `./1' rmdir: removing directory, `.' rmdir: removing directory, `./2/3' rmdir: removing directory, `./2'
share|improve this answer
    
recursively like This does work on one line, but I want to keep deleting empty folders until there's no true empty folder. –  kmassada Apr 14 '13 at 13:02
    
@kmassada: use rmdir -p –  Iain Apr 14 '13 at 13:14
add comment

Your Answer

 
discard

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.