Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Getting all extensions for a directory: easy. Getting file counts for a particular extension: easy.

But getting all file extensions and their respective file counts is alluding me.


+ dir
 + abc.txt
 + def.txt
 + abc.pdf
 * def.pov

should return something like:

.txt 2
.pdf 1
.pov 1

The aim of this exercise is that I want to find out which file extension is popular in a certain directory.

Thanks in advance

share|improve this question
up vote 30 down vote accepted
/var/cache$ sudo find ./ -type f | grep -E ".*\.[a-zA-Z0-9]*$" | sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/' | sort | uniq -c | sort -n
      1 .6
      1 .cache
      1 .noconf
      1 .php
      1 .sl
      2 .bin
      2 .el
      2 .tdb
      4 .baseA
      4 .baseB
      4 .dat
      4 .DB
     27 .db
    221 .deb

find ./ -type f

find only file, not directory

grep -E ".*\.[a-zA-Z0-9]*$"

filter file with extension

sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/'

delete path and file name, save only extension

sort | uniq -c | sort -n

sort, uniq and sort

share|improve this answer
nice. perfect! love. thanks – denormalizer Sep 22 '10 at 5:55
You could make your regex allow more characters in the extension and eliminate grep by doing this: sed -ne '/\.[^./]*$/s/.*\(\.[^.]*\)$/\1/p' – Dennis Williamson Sep 22 '10 at 6:17
Dennis, replacing the grep and sed with your sed returns the following error: sed: -e expression #1, char 30: invalid reference \1 on `s' command's RHS – denormalizer Sep 23 '10 at 1:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.