4

How do I recursively count all files of a certain type in zsh?

There are quite a few methods to do this (helpful SuperUser questions such as this one give pointers), but few zsh-specific methods.

This follows my previous question - several zsh-specific solutions have been given to me, so I will record them here in case anybody else ever wants to do this.

simont
  • 1,412

2 Answers2

5

The zsh-specific feature is zsh globbing; I don't fully understand it, but these work.

  1. $ a=( **/*.(cpp|h)(.) ); print $#a

    Will count all files ending in .cpp and .h recursively from the current directory, then print the result as a single number.

  2. From this answer, I can also use:

    $ ls **/*.{cpp,h} | wc -l

The zsh specific part, then, is the expansion of **/*.{cpp,h} to match files ending in .cpp and .h. I haven't yet found any (simple) comprehensive of zsh globbing that I've been able to understand (although this explains the use of **/*.{a,b} fairly well).

simont
  • 1,412
1

To count no matches correctly: a=( */.(cpp|h)(.N) ); print $#a

To avoid leaking the variable: local -a a; a=( */.(cpp|h)(.N) ); print $#a

Graham
  • 11