Sometimes it can be useful to know how many files of a particular type are in a directory. This post presents a single line command and a shell function that count the number of files with a given extenstion in the current directory. Directories are searched recursively. A version of the shell function in a stand alone script is also presented.

Software Versions

$ date -u "+%Y-%m-%d %H:%M:%S +0000"
2017-01-25 09:46:14 +0000
$ uname -vm
FreeBSD 12.0-CURRENT #18 4f888bf(drm-next): Wed Jan 18 14:31:26 UTC 2017     root@gauntlet:/usr/obj/usr/src/sys/GENERIC  amd64

Instructions

The basic command to recursively tally files with a given extension looks like this.

sh

EXTENSION=.sh
ls -lR | grep "${EXTENSION}" | wc -l

The can be used as the basis of a shell script.

countfiles.sh complete listing

#!/bin/sh

countfiles() {
  for extension in $@
  do
    count=$(ls -lR | grep "${extension}" | wc -l)
    printf "${extension}\t${count}\n"
  done
}

countfiles $@

The shell script can be used as follows.

chmod +x countfiles.sh
./countfiles.sh .java .kt .sh

Output will look something like this.

.java	     892
.kt	       2
.sh	     251

Obviously, the countfiles function can be entered in the shell or .profile. In that case it can be used as follows.

.profile partial listing

countfiles() {
  for extension in $@
  do
    count=$(ls -lR | grep "${extension}" | wc -l)
    printf "${extension}\t${count}\n"
  done
}

sh

countfiles.sh .java .kt .sh

Obviously, the output is exactly the same as above.