Recursively delete empty folders with verbose output
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'
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'