On Thu, Sep 27, 2018 at 09:59:58AM -0400, Greg Wooledge wrote:
All of the above give the wrong answers when filenames contain newlines.
Any solution that involves printing the filenames to a stream and then
trying to parse that stream to guess how many filenames are in the stream
is a non-starter -- unless of course the stream uses NUL delimiters
instead of newlines.
Sadly, the GNU coreutils maintainers have rejected every request, even
requests with patches attached, to add a --null option to ls. So,
ls is not suited to this task.
Correct, find would be a better choice for programmatic use. E.g.,
something like
find . -mindepth 1 -maxdepth 1 -printf '\n' | wc -l
ls should be considered a user interface, not an API. If ls were
modified sufficiently to provide a rigorous programatic interface, it
would basically become a very non-portable reimplementation of find.
Mike Stone