Get counts of directory depth levels with find, awk, uniq and sort

Let's say we've got a bunch of directories and we'd like to get counts of how many directories are how many levels deep.

Our directory structure:

$ find . -type d

There are a number of ways to do this, but I found awk to be most elegant as I wanted to be able to substract an arbitrary number from the result and awk can count and do arithmetic all by itself:

$ find . -type d | while read f; do awk -F'/' '{print NF -1}' | sort | uniq -c | sort -nr; done
      3 2
      3 1
      2 3
      1 4

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).