Search Posts on Binpipe Blog

Find Total Number of Sub-directories and Files in a Directory

We have often wondered how many sub-directories and files are there recursively under a directory in Linux. Sometimes, we may also need to calculate the number so that we don't cross inode count in shared hosting servers.

The following code snippet will allow you to get the count of files & directories inside a folder. Please run this command after doing a 'cd' to inside the folder.

find $DIR -exec stat -c '%F' {} \; | sort | uniq -c | sort -rn

If you want a tree view of the directories with the count you can use this:

for i in $(ls -d */); do tree  $i ; done > result.txt

or try this one below:

for i in $(ls -d */); do tree $i | grep -v \\-\\-\  ; done

Quite simple but effective one-liners aren't they?  :-)

No comments:

Post a Comment

Hi, Leave a comment here and one of the binary piper's will reply soon :)