$ cat tmp/example.awk
{
a[$1]++
} END {
for (i in a) {
dir=i; gsub(" ", "\\ ", dir) ;
printf "echo %s: %d; cd %s; ls -1 *htm; cd ->/dev/null; echo\n", \
i, a[i], dir, dir
}
}
Sample output:
find . -iname "*.htm" -printf "%h\t%f\n" |sort |\
awk -F'\t' -f tmp/example.awk |sh
(...)
./fpsu/applikasjon/kjerne/lib: 1
xom-1.0b5.htm
./tmp/refcp/refcp/home/jaroslav/reference/programing/input-processing: 1
Capitalizing+personal+names.htm
./Downloads/index files: 3
allclasses-frame.htm
overview-frame.htm
overview-summary.htm
./tmp/refcp/refcp/home/jaroslav/reference/code/html-css/common-web-fonts_files: 6
hostedbadge_002.htm
hostedbadge_003.htm
hostedbadge.htm
like_002.htm
like_003.htm
like.htm
(...)
The first step is to print every folder and file name with a given extension. That is accomplished with the %h %t arguments to find's printf.
The next step is to count every entry in a given folder with a[$1]++ in the awk program. That is, for every line printed, we increment an array entry with the index equal to the name of the directory found containing files with the given suffix.
When all entries have been counted we echo a simple shell command to print the results and list the files in every directory that we found.
The result is a list of shell commands that we pipe into sh
.