Sub directories containing maximum files

Hi All,

I have this command coded in C Shell to get the top ten sub directories in the order of number of files they contain.

find $parent_dir -type d -exec filecount {} \; | sort -nr | head -10

But it does not seem to show any output. Can someone please help me out in correcting this (preferably one liner command am expecting).

Thanks
-Durga

% du <DIR> | sort -nr | head -10

But this will give the directories occupying maximum space. What my command is intended is to get the directories containing maximum number of files (size may be of smaller but count wise they will be high)

$ Desktop/Scripts/dir_count.sh | sort -k 2 -nr | head -10
./.cache/google-chrome/Default/Cache 627
./sybaseDB/jConnect-6_0/docs/en/progref 198
./.compiz/session 183
./sybaseDB/shared/JRE-6_0_6_32BIT/lib/zi/America 110
./sybaseDB/jre32/lib/zi/America 110
./sybaseDB/charsets/unicode 94
./sybaseDB/jConnect-7_0/sample2 79
./sybaseDB/jConnect-6_0/sample2 79
./sybaseDB/shared/JRE-6_0_6_32BIT/lib/zi/Asia 78
./sybaseDB/jre32/lib/zi/Asia 78



$ cat Desktop/Scripts/dir_count.sh 
find . -type d | while read dir; do 
    count=$(find "$dir" -maxdepth 1 -type f | wc -l)
    echo "$dir $count"
done

Excuse me. You need a simple program "filecount". Something like:

#!/bin/sh
echo $(ls $1 | wc -l) $1

Put this in file, name it "filecount", chmod +x filecount, put this command in your path (for example in $HOME/bin, and add $HOME/bin to PATH), then your find command should work.

Thanks Yazu and itkamraj.
I missed the executable filecount. I was under the impression that filecount is a UNIX command. Found out the missing filecount script and now the command works fine. :slight_smile: