如何获取所有文件中出现的单词?但是每个目录的单词数而不是单个数字

How to get occurrences of word in all files? But with count of the words per directory instead of single number

我想在所有文件中得到给定的字数,但每个目录而不是单个字数。我确实通过转到特定目录来使用简单的 grep foo error*.log | wc -l 来计算字数。当目录结构如下所示时,我想获得每个目录的字数。

Directory tree                 
.
├── dir1
│   └── error2.log
    └── error1.log
└── dir2
     └── error_123.log
     └── error_234.log
 ── dir3
     └── error_12345.log
     └── error_23554.log

更新:在AIX上可以使用以下命令:

#!/bin/bash

for name in /path/to/folder/* ; do
    if [ ! -d "${name}" ] ; then
        continue
    fi
    # See: https://unix.stackexchange.com/a/398414/45365
    count="$(cat "${name}"/error*.log | tr '[:space:]' '[\n*]' | grep -c 'SEARCH')"
    printf "%s %s\n" "${name}" "${count}"
done

在 GNU/Linux 上,使用 GNU findutils 和 GNU grep:

find /path/to/folder -maxdepth 1 -type d \
    -printf "%p " -exec bash -c 'grep -ro 'SEARCH' {} | wc -l' \;

用实际搜索词替换 SEARCH