Display the number of files in a directory and recursively in each subdirectory

Display the number of files in a directory and recursively in each subdirectory

To look something like below, for example

/var          35
/var/tmp    56
/var/adm   46

Any ideas how can we do this?

Got a sun cluser global mount point which takes ages to mount everytime, need to understand the file distribution to see if we can split up the filesystem.

That phrasing of the question sounds like homework/classroom stuff?

Please define "number of files". Does it include directory files?
Please post what Operating System and version you have and what Shell you prefer.

You can do with something like:

for i in `find ./ -type d`;
do 
   echo $i `find $i -maxdepth 1 -type f|wc -l`;
done|cut -b 2-

Not very efficient but it works...

If I run it in my /usr

/ 0
/share 1
/share/opencc 23
/share/grub 7
/share/grub/default 2
/share/unity-2d 0
/share/unity-2d/shell 4
/share/unity-2d/shell/artwork 0
/share/unity-2d/shell/common 10
/share/unity-2d/shell/common/visibilityBehaviors 4
/share/unity-2d/shell/common/artwork 15
/share/unity-2d/shell/launcher 6
/share/unity-2d/shell/launcher/artwork 17
/share/unity-2d/shell/dash 33
/share/unity-2d/shell/dash/artwork 14
/share/unity-2d/shell/hud 2
/share/unity-2d/panel 0
/share/unity-2d/panel/applets 0
/share/unity-2d/panel/applets/appname 0
/share/unity-2d/panel/applets/appname/artwork 13
/share/unity-2d/spread 6
/share/libdbi-perl 1
/share/guile 0
/share/guile/1.8 1
/share/guile/1.8/oop 1
/share/guile/1.8/oop/goops 12
/share/guile/1.8/lang 0
/share/guile/1.8/lang/elisp 5
/share/guile/1.8/lang/elisp/internals 10
/share/guile/1.8/lang/elisp/primitives 19
/share/guile/1.8/ice-9 56
/share/guile/1.8/ice-9/debugger 5
/share/guile/1.8/ice-9/debugging 6
/share/guile/1.8/srfi 22
/share/xdiagnose 3
/share/xdiagnose/icons 1
/share/xdiagnose/workloads 16
/share/myspell 0
/share/myspell/infos 0
<truncated>

@juan.brein
What version of Solaris/SunOS do you have and what Shell are you using?

I did it on bash under Linux, now I realize the Open Source "find" implementation has some options that Solaris doesn't, like -maxdepth...

You can use this alternative always using bash:

Now on a Solaris 10 box under /usr:

/ 0
/lost+found 0
/share 3
/share/javadoc 0
/share/javadoc/smartcard 11
/share/javadoc/smartcard/com 0
/share/javadoc/smartcard/com/sun 0
/share/javadoc/smartcard/com/sun/smartcard 0
/share/javadoc/smartcard/com/sun/smartcard/scf 17
/share/javadoc/smartcard/resources 1
/share/javadoc/imq 14
/share/javadoc/imq/com 0
/share/javadoc/imq/com/sun 0
/share/javadoc/imq/com/sun/messaging 25
/share/javadoc/imq/com/sun/messaging/jms 21
/share/javadoc/imq/com/sun/messaging/xml 5
/share/javadoc/imq/javax 0
/share/javadoc/imq/javax/jms 61
/share/javadoc/imq/javax/xml 0
/share/javadoc/imq/javax/xml/messaging 12
/share/javadoc/imq/javax/xml/soap 28
/share/javadoc/imq/resources 1
/share/lib 11
/share/lib/jdmk 2
/share/lib/mailx 2
/share/lib/pub 4
/share/lib/tabset 10
/share/lib/terminfo 0
/share/lib/terminfo/3 18
/share/lib/terminfo/A 82
/share/lib/terminfo/a 289
<output truncated>