Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Fastest way to count # of files?



On Thu, Sep 8, 2016 at 9:37 AM, Sebastian Gniazdowski
<sgniazdowski@xxxxxxxxx> wrote:
> First ran was slower than this and following ones:
>
> % typeset -F SECONDS; myst=$SECONDS; integer nfiles=0; :
> **/*(Ne?'((++nfiles)) && reply=()'?); print $nfiles; echo $(( (SECONDS
> - myst) * 1000 ))
> 80001
> 1427.3609999982
>
> I.e. 1.4 second

It may be using more time than your original example because of the
**/ recursion.

The only other option I can think of is this:

integer nfiles=0
typeset -a nlink
zmodload zsh/stat
: **/*(/Ne?'zstat -A nlink +nlink $REPLY && ((nfiles += $nlink - 2));
reply=()'?)
print $nfiles

This assumes that nlink for a directory is the number of files it
contains plus "." and "..".  However, the result I get is off by a
little from the glob without directory filtering.



Messages sorted by: Reverse Date, Date, Thread, Author