1

Supposing this file system structure:

ROOT
    DIR1A
        FILE
        DIR2A
        DIR2B
            DIR3A
    DIR1B
        DIR2C
        DIR2D
            DIR3B
    DIR1C
        DIR2E
            FILE

Starting from an arbitrary directory, how can I list only the shallowest of it's child directories which in turn contain either a) nothing or b) only empty directories all the way down, but without listing said empty children?

That is, in the case above, if I started at ROOT:

  1. DIR1A would NOT be listed, because it contains a file.
  2. DIR2A WOULD be listed, because it contains nothing.
  3. DIR2B WOULD be listed, because it contains only empty directories.
  4. DIR3A would NOT be listed, because it is within a shallower directory that's already been listed.
  5. DIR1B WOULD be listed, because it contains only empty directories.
  6. Children of DIR1B would NOT be listed, because they are within a shallower directory that's already been listed.
  7. Both DIR1C and DIR2E would NOT be listed, because there's a file nested in there.

I'm confident there's a more efficient way to say this. Perhaps "I want to list only the highest-order directories which contain either nothing or solely empty directories, all the way down"?

EDIT: I attempted to clarify some of the language above.

  • Can you elaborate this Children of DIR1B would NOT be listed, because they are within a listed parent? – RomanPerekhrest Mar 23 '18 at 15:41
  • Yes--since DIR1B contains no FILES (in this case, some empty directories, yes, but no files) all the way down, IT is listed. But its empty-dir children are not listed because they are contained by a listed dir Perhaps I should reword this as "Starting at an arbitrary directory, I want to recursively list every directory that is either empty or contains only empty directories, but without listing any of the children of those directories." – bland328 Mar 24 '18 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.