I want to show this kind of statistics about a particular directory:

$ myscript.sh
.c 34265 files
.h 17866 files
.cbl 1804 files
.pco 389 files

Is there a tool that can do this?

Here is my attempt:

find . -name "*.*" | sed -e "s/.*\.//g" | sort -u

And then I could type for each type find . -name "*.pc" | wc -l but it is bothersome so I wrote:

find . -name "*.*" | sed -e "s/.*\.//g" | sort -u |\
    xargs find . -name "*.{}" | wc -l

Unfortunately I must be missing something because it says:

find: paths must precede expression: as

("as" is the first extension)
Any idea what is going wrong?
Is there already a tool that does what I want (a tool counting file lines would be OK too).

3 Answers 3


It can be done with gawk: pipe your list of filenames through the following (left as an exercise/I tested with ls -1, but Don't Parse ls(tm)):

gawk -F. '1 {exts[$NF]++} END {for (ext in exts){printf "%s: %s files\n", ext, exts[ext]}}'

This assumes that everything that is followed by a period is not part of the extension, so filenames without period count as all-extension. (I think it should be sufficient to change 1 { to (NF>1) { to only get files with extensions proper.

  • That works! Here is the complete command: find . -name "*.*" | gawk -F. '(NF>1) {exts[$NF]++} END {for (ext in exts){printf "%s: %s files\n", ext, exts[ext]}}' Feb 17, 2012 at 7:26

The following pipeline prints each distinct line in the input preceded by its repeat count, sorted by decreasing repeat count:

sort | uniq -c | sort -rn


find . -type f -name '?*.*' | sed 's/.*\.//' | sort | uniq -c | sort -rn
ls -1 | awk -F. '{if(NF>1)arr[$NF]++;else blnk++}END{for(i in arr)printf(".%s %s file(s)\n",i,arr[i]);printf("<No Extension> %s file(s)\n",blnk)}'
  • 2
    Using ls is typically not a good idea (ie. it is a bad idea). eg. it prints a line in which the link's target's extension is returned. So the awk filter reports on the wrong extension.. It will also pick up directory names which have a dot in their names.. Aside from the mentioned and other complications caused by ls, I like the awk logic.. (and it appears that he wants the output sorted by the number of occurrences)
    – Peter.O
    Feb 17, 2012 at 10:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .