Welcome to WebmasterWorld Guest from

Forum Moderators: bakedjake

Message Too Old, No Replies

count files in a directory and each subdirectory



5:53 pm on Sep 18, 2006 (gmt 0)

5+ Year Member

Has anybody written a short shell script that could be launched in any directory and it would list all the subdirectories and how many files in each specific subdirectory?


5:58 pm on Sep 18, 2006 (gmt 0)

WebmasterWorld Senior Member lammert is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Seems to be a popular topic:



6:15 pm on Sep 18, 2006 (gmt 0)

5+ Year Member

That was a total count. I was looking for directory by directory file count so you could see how many files were in any subdirectory.

This will have to be a shell script in a for loop, not a quick one liner becuase the loop will first have to identify the subdirectory, then do the count, and then print subdirectory name and file count to the screen.


7:46 pm on Sep 18, 2006 (gmt 0)

5+ Year Member

Couldn't wait. wrote it myself. Here it is in case anybody else finds themself with a Unix box running out of space.

#Counts files in subdirectories
date_stamp=`date +"%m%d%Y.%H%M%S"`
find . -type d > $temp_file

exec 3< $temp_file
until [ $done ]
read <&3 myline
if [ $?!= 0 ]; then
dircount=`find $myline"/." \( -name . -o -prune \) -type f ¦ wc -l`
echo "$dircount"$myline
rm $temp_file


Featured Threads

Hot Threads This Week

Hot Threads This Month