Number of files per directory

Hi Guys,

I am trying to display the total number of files within a directory (including sub dirs): i.e.

solaris>  du -sh * | sort +0rn 
1009K   cron
 940K   lib
 849K   svc
 489K   cacao
 261K   opt
 212K   preserve
 167K   dt

But instead of the size I would like the number of files and there equiv dir.

Thanks in advance!!!!:D:o:b::):confused::rolleyes::eek::cool::wink:

Dont know...
I would use

ls -lR| wc -l

But the proper way would be to use find, so look at the man pages of find, especially -type option

that would give me the entire number of files in the dir and recursivly as a single interger. not per directory basis i.e.

Directory Number of Files (incl sub dirs)
/var 112312
/etc 35210

and so on

but thanks for responding tho

---------- Post updated at 11:20 AM ---------- Previous update was at 11:14 AM ----------

I have found this command that works on Linux (RHEL5) but doesnt work on Solaris and also is too granular and indicates the size of each sub dir and doesnt just give me the total instead:

find . -type f | awk '{dir=gensub(/(.+\/).+/,"\\1","g",$0); dir_list[dir]++} END {for (d in dir_list) printf "%s %s\n",d,dir_list[d]}' | sort

One approach:

#!/bin/ksh
(
find . -type d -print|sort|while read DIR
do
        FILES_TOT=`find "${DIR}" -type f -print|wc -l`
        echo "${FILES_TOT} ${DIR}"
done
) | sort -n -r

or awk -

find /path -type f -exec dirname {} \; | 
    awk -F '/' {arr[$2]++} END { for (i in arr) { print i, arr } }' | sort

for top level directories only

Thanks for your response guys:

Meth: thats good! but is there a way that it can just give my the total number of files within the dir instead of each one? group them together ie. from root: /var 100234 which includes all of /var subdirs. /etc 1323423 which includes all /etc subdirs.

Jim: I keep getting an error with your awk version:

bash: syntax error near unexpected token `(i'
syntax error near unexpected token `('

nice work btw.

This would seem to be closer to your requirement. First find the names of the directories then count the number of files (excluding anything which is not a file) in each top level directory. If you are counting files in areas like /usr and /var it is usually necessary to stop "find" following links into other filesystems.

#!/bin/ksh
(
ls -lad ./*|grep \^dr|awk '{print $9}'|while read SUBDIR
do
        COUNTER=`find "${SUBDIR}" -xdev -type f -print|wc -l|sed -e "s/ //g"`
        echo "${COUNTER} ${SUBDIR}"
done
) 2>&1 | sort -n -r

You're a star! Thank you so much! How can I award you points or stars? or a beer?

#!/someposixsh

# number of regular files in directory
currdir=$PWD
for dir in $(find .  -type d)
do
        cd $currdir/$dir
        files=$( find .  -maxdepth 1 -type f | wc -l )
        echo "$files $dir"
done | sort -k 1,1nr
echo ____________________________________________________________________
# number of regular files in directory+subtree
cd $currdir
for dir in $(find .  -type d)
do
        cd $currdir/$dir
        files=$( find .  -type f | wc -l )
        echo "$files $dir"
done | sort -k 1,1nr

this is what I would do:

find /bigdir -type d -print | while read D
do
echo "$D\t `ls -l $D | grep -c -v ^D`"
done