On 06/07/2025 12:11, Stefan Klinger wrote:
Hello,

I've implemented a small tool [1] to test whether a directory is
empty, and I think it would make a useful addition to GNU coreutils.

There once was a discussion [2] on this mailing list, proposing the
extension of `test` with a `-E` flag.  AFAICT this led nowhere,
although the topic does not show up as rejected feature request [5].

The need to test a directory for emptiness obviously arises
occasionally [3], the proposed solutions vary in quality and tend to
invoke discussion [4].

IMHO, the number of tools required to implement such a simple test
should be 1, but proposed solutions typically involve an invocation of
`ls` or `find` and subsequent output processing by `test` or `wc`.
These solutions tend to accumulate a degree of complexity that I
personally find unsatisfying for the task at hand.

Some solutions are more compact and use only shell builtins, e.g.,

     $ if ! (shopt -s failglob dotglob; : *) 2>/dev/null; then
           echo is empty
       fi

but this may not port nicely to other shells (and has some other
caveats).

I would find `isemptydir` to fit well with the family of commands like
`basename`, `pwd` & Co. — While an addition of theis functionalit to
`test` would feel natural, the question of which `test` (the one from
coreutils, or a shell builtin) arises.  There's no need to reimplement
functionality in every shell.

I have built `isemptydir` according to my own needs and standards,
which may not line up with the culture at GNU/coreutils.  But if
you're at all interested, then I'm willing to adapt this (e.g.,
writing a manpage, follow commit log requirements, coding style,
etc.).  Obviously, this effort only makes sense if adoption is indeed
desired.  If so, any feedback on the existing codebase would be
appreciated.

Kind regards
Stefan


[1]: https://github.com/s5k6/isemptydir
[2]: https://lists.gnu.org/archive/html/coreutils/2016-04/msg00024.html
[3]: 
https://www.startpage.com/sp/search?query=linux+shell+test+directory+is+empty&cat=web&pl=opensearch&language=english
[4]: 
https://superuser.com/questions/352289/bash-scripting-test-for-empty-directory
[5]: https://www.gnu.org/software/coreutils/rejected_requests.html

Thanks for taking the time to propose a patch and reference previous 
discussions.

Re [5], indeed it wasn't rejected,
but I agree with the thread at [2] that `test -F` may be the best interface.
That would be best broached with POSIX folks, or shell folks at least.

Micheal Stone's request for a concrete use case would be useful,
as personally I've only needed `rmdir --ignore-fail-on-non-empty`.
Anyway I don't think a separate utility is warranted for this functionality.

thanks,
Padraig

Reply via email to