count-files-in-directories.sh 469 B

1234567891011121314151617
  1. #!/bin/bash
  2. # Count the files each directory in a heirarchy
  3. #
  4. # USAGE: count-files-in-directory.sh [DIR [FILETYPE]]
  5. STARTDIR=${1:-.}
  6. FILETYPE=${2:-""}
  7. # Use find to get all directories in the directory structure
  8. find $STARTDIR -type d -name \*${FILETYPE} | while read -r dir
  9. do
  10. # Use wc to count the number of files in each directory
  11. num_files=$(find "$dir" -type f | wc -l)
  12. # Print the directory and the number of files
  13. echo "$num_files $dir"
  14. done | sort -n -r