Count files in all subdirectories, by directory

By jbayer - Last updated: Friday, June 22, 2012 - Save & Share - Leave a Comment

This is a quickie. I had a need to see how many files were in each directory (not including files in directories underneath)

This little script will do just that, and save the output in a file called “b.csv” (I was lazy).  The file will be formatted as a comma separated csv, which you can load into any spreadsheet:

#!/bin/bash
dir=`pwd`
 [ $# -ne 0 ] && dir=$1
 echo $dir
#echo "ls -1 $1 | wc -l" >b.sh
 echo "find . -maxdepth 1 -type f -print | wc -l" >b.sh
 chmod +x b.sh
 find $dir -type d -print -exec ./b.sh {} \; | awk '{printf("%s%s", $0, (NR%2 ? "," : "\n"))}' >b.csv

 

Posted in Bash, Programming • • Top Of Page
999 views

Write a comment

asd