How do I delete all empty directories
(on a Linux system)
I have a large set of nested subdirectories on a filesystem. I would like to prune all directory paths that contain no files at all.
In other words I would like to delete every directory where there are no files in that directory or in any subdirectory of that directory recursively.
For all versions of find
find -depth -type d -empty -exec rmdir {} \;
If you have a newer version
find -type d -empty -delete
May not be the best solution, but this script works:
#!/bin/sh
while true
do
DIRS=`find . -xdev -type d -exec find {} -maxdepth 0 -empty \;`
if [ -z "$DIRS" ]; then
exit 0
else
echo $DIRS | xargs rmdir
fi
done
(based partly on the answer to List all empty folders)