There's the find . -type d -empty
command, but that only finds literally empty directories.
What I want to achieve is something a little bit more complex: I want to find empty directories or directories that ONLY have other directories or empty directories, and this rule should be passed on recursively.
So for example, in this directory structure:
~1/11/111/
It would find 1/
, 1/11/
and 1/11/111/
But if at any point in the tree there's a file, for example in this case if there's a file in 1/11/111/file1
then none of them before the file should be returned. So if there's an empty directory 1/11/111/1111/
next to 1/11/111/file1
, 1111 should be returned.
The reason I want this is because I want to delete my empty folders.
Now I could probably do that by running find . -type d -empty -delete
over and over again, but I want a way to kind of visualize it before I remove them.
Is this possible?
find . -depth -type d -empty -delete
might even do – ilkkachu Jul 23 '19 at 07:47-depth
is implicit with-delete
. – Kusalananda Jul 23 '19 at 07:48find
throughsort
into an awk-script to filter out directories having content (take a half hour or so, which is longer than I'd spend for this category). – Thomas Dickey Aug 29 '20 at 12:51