Fri, 9 Nov 2007

Finding File and Directory Counts

— SjG @ 3:31 pm

So, in the process of organizing photographs, I wanted to examine my deeply-nested hierarchy to figure out how it’s possible I have 30,000 images (Aperture only wants me to have 10,000 in a project, so I need to re-organize the hierarchy even before I import).

So, I figured it’d be easy to use find to list all my directories, and how many images they contain. It turns out that (at least for me) it’s not.

My best stab so far is to use find and a loop, which gives me almost what I want (it not only includes the count of images in the each directory, but subdirectories as well). It fails if there are too many directories. It’s good enough. But it’s not elegant.

So CLI Deities — how would you make this pretty?

find . -type d | while read dir; do echo `ls -1 "$dir" | wc -l` $dir; done

Potential type-face issue disambiguation: after the ls, that first argument is a one, not an ell, although I suppose an ell would work too. The wc option is an ell.

2 responses to “Finding File and Directory Counts”

  1. SjG says:

    Karl suggested:

    for NAME in `find . -type d`; do (RESULT=`find $NAME -maxdepth 1 -type f | wc -l`; echo $RESULT $NAME); done

    which isn’t bad. I should add that second find instead of my “ls -l”

  2. SjG says:

    Hm. Also noticed that this throws errors on directories whose names end in a space. WTF?!? WTF am I doing with a space at the end of a directory name? Fer cryin’ out loud.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.