Web lists-archives.com

Re: question about ls

On Thu 27 Sep 2018 at 09:59:58 (-0400), Greg Wooledge wrote:
> On Thu, Sep 27, 2018 at 08:53:28AM -0500, David Wright wrote:
> > On Thu 27 Sep 2018 at 07:55:56 (-0500), Kent West wrote:
> > > westk@westkbox:/opt$ ls -la | wc
> > >      7      56     321
> > > westk@westkbox:/opt$ ls -1a | wc
> > >      6       6      54
> > 
> > But do use 1A, not 1a, if you want to know how many items are
> > in a folder, otherwise . and .. will be included in the count.
> 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.

Sure, your FAQ is an invaluable resource for scripting, especially in
a "hostile" environment. But I think one also needs to be aware of
practical commands for use at the commandline in a benign environment.
Knowing which technique is appropriate comes with experience.

One has to be aware of the limitations of all the tools one uses.
Just as ls has weaknesses, so does wc if misused. Most people assume
that -l counts the lines in a file, but actually it counts the
newlines, which matters for the last line.

But here's another plug, and there are lots more goodies there too: