Find the directories containing the most files

Posted by jason on Mar 27, 2009 in hacks |

I needed to find the directories containing the most files, so I whipped up this bit of voodoo at the command line:

find . -type f -print | perl -na -F/ -e 'while (pop @F && @F) { print join ("/", @F)."\n"; }' | sort | uniq -c | sort -n

This will count the number of files in each directory beneath your current directory, and print out those directories in ascending order by the number of files they contain. It will also print totals for parent directories–if “foo/bar” has one file, “foo/baz” has two, and “foo/” itself has one, then the line for “foo” will be printed with the number four.

Handy for me, possibly handy for you as well. This works in Linux and Mac OS X Leopard.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Print

Tags: , , , ,

3 Comments

Joe
Mar 28, 2009 at 5:10 am

Would something along these lines this do what you wanted?

for d in */; do set — “$d”*; printf “%s: %d\n” “${d%/}” “$#”; done

I don’t like needing perl in there… :)


 
jason
Mar 28, 2009 at 8:17 am

I know what you mean–I didn’t want to have Perl in there, either. I initially tried to hook something up using only find, xargs, and wc, but couldn’t get the pipes to work out how I wanted them.

I tried something like this:
find . -type d -print0 | xargs -0 -I {} find {} -print | wc -l

…but I needed the second pipe to apply to the second find, not to xargs. I couldn’t seem to hook that up.


 
jason
Mar 28, 2009 at 8:18 am

Oh, and the one you supplied only goes down one level, I think–it doesn’t recurse. :-)


 

Reply

Copyright © 2010 drjason.com All rights reserved. Theme by Laptop Geek.