1

I have a directory with a very large number of subdirectories (~800) that were generated programmatically. I want to get a count of the number of files in each of these subdirectories to check for anomalies (if the code broke on a run then some of the files will be missing). What's a quick way to do this? The sort of output I'm looking for is:

Name_of_Folder_1 [# of files in Folder 1]   
Name_of_Folder_2 [# of files in Folder 2]
...
mikefallopian
  • 115
  • 1
  • 5
  • 1
    It's rarely used, but `diff` will happily report the differences between folders. `diff folder1 folder2`. – steve Aug 12 '15 at 20:30
  • 1
    See also [How can I get a count of files in a directory using the command line?](http://unix.stackexchange.com/q/1125) and there are more related questions if you take the time to search the site. – don_crissti Aug 12 '15 at 20:41

2 Answers2

2

This will get you the count of files in each subdirectory of the current directory, dealing with any strange file names (with gnu find).

find . -maxdepth 2 -mindepth 2 -type f -printf "%h\0" | uniq -zc | tr '\0' '\n'
Matt
  • 8,841
  • 1
  • 26
  • 32
1

Assuming that you have no spaces in your directory names:

for dir in $(find . -type d); do
    echo "${dir}: $(find ${dir} -maxdepth 1 -type f | wc -l)"
done
Andy Dalton
  • 13,654
  • 1
  • 25
  • 45