How to calculate and print number of files in each subfolder of the current folder on Linux

find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr

That is: find all subdirectories of current directory; for each subdirectory, find all files, and print file count and path. Finally it sorts the result to show folders with most files on top
Note that the first entry is the folder itself. Also this script omits soft links and does not count sub-folders as files.

For example:

[someone@somewhere]# cd /etc/X11/
[someone@somewhere]# find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr
10 .
7 ./xinit
3 ./xinit/xinitrc.d
0 ./xorg.conf.d
0 ./xinit/Xclients.d
0 ./fontpath.d
0 ./applnk

Advertisements
How to calculate and print number of files in each subfolder of the current folder on Linux

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s