Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

I am trying to search the filesystem for all files matching an extention (*.what), then count all files in the directory where it found the *.what files. The output should contain the directory name and filenames with count.

How can this be done?

share|improve this question
cd /folder find -name *.extension this will list all files with the extension – Ajo Mathew Nov 26 '12 at 17:33

First you would use the find command in the terminal.

find . -type f -name "*.what"

That will list all files on the system from the current directory "." matching "type: file" and name "*.what".

So you can incorporate that into a bash script, like so:


Here you go, this does what you want I think.



for dir in `find ${src} -type f -name "*.${ext}"`; do
    dir=`echo ${dir} | awk 'BEGIN{FS=OFS="/"}{$NF=""}{print}'`
    echo ${dir} "has" `ls -l ${dir} | awk '!NR=1 && !/^d/ && !/*.what/ {print $NF}' | wc -l` "file(s)"

That will output the number of files in any directories that contain *.what (recursively). The number if files excludes directories !/^d/ and the *.what file !/*.what/.

That should get you there. Works on my system at least, assuming I understand the question.

share|improve this answer
hmm, something not working quite right, the script only outputs 1 file found then ends. Its not searching through all the directories and subdirectories. Just to reiterate, I want to find any file ending with *.what and then count the number of all files in that same directory, not just counting the *.what files in the pwd. – Cyw Nov 26 '12 at 19:17
That's because I misunderstood, it is currently just counting the number of *.what 's. I will update in a few. – nerdwaller Nov 26 '12 at 19:29
@Cyw updated above^^ – nerdwaller Nov 26 '12 at 19:58
Thanks a lot! Looking better, I'm still not getting the results and am getting error "awk: xregcomp: Invalid preceding regular expression" Could be due to its VMware ESXi 5.x system running AWK from BusyBox v1.9.1-VMware-visor-8630 (2012-01-06 01:09:05 PST) multi-call binary ?? – Cyw Nov 26 '12 at 20:16
Yeah, awk may behave differently under busybox. Try breaking it down to see which it is, or both uses. – nerdwaller Nov 26 '12 at 20:23

Try this pipeline:

find -name '*.what' -exec dirname {} \; | sort | uniq -c
share|improve this answer
I need this to count the files in any directory where a *.what file is found and if the count is less than some #, display the pwd and filename and total file count within that directory. This needs to be a recursive search. Thank you – Cyw Nov 26 '12 at 19:20
$ cat tmp/example.awk
} 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

./tmp/refcp/refcp/home/jaroslav/reference/programing/input-processing: 1

./Downloads/index files: 3

./tmp/refcp/refcp/home/jaroslav/reference/code/html-css/common-web-fonts_files: 6

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.

share|improve this answer
find -name *.c|grep -c .c

this will find the number of items with extension .c

find -name *.c 

will list the directory structure of items with extension .c


find -name *$1|grep -c $1
find -name *$1

save this as in /home/yourname/bin

Chmod +x
chown {username}

call it anywhere by list *.extension

Example list *.c

share|improve this answer
I like this one. But you need to make @cYrus's suggestion. – nerdwaller Nov 26 '12 at 17:45
You should quote *.c. – cYrus Nov 26 '12 at 17:47
using ubuntu it worked with out the quote .. Simple script #!/bin/bash find -name *$1|grep -c $1 find -name *$1 – Ajo Mathew Nov 26 '12 at 17:51
Updated the answer simple way – Ajo Mathew Nov 26 '12 at 17:56
This is good start but I need a way to count all files in any directory that a file with *.what is found and display the filename & directory name and the total file count within that directory that the *.what file is found – Cyw Nov 26 '12 at 19:18

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.