How to count number of files in each directory?
Assuming you have GNU find, let it find the directories and let bash do the rest:
find . -type d -print0 | while read -d '' -r dir; do
files=("$dir"/*)
printf "%5d files in directory %s\n" "${#files[@]}" "$dir"
done
find . -type f | cut -d/ -f2 | sort | uniq -c
find . -type f
to find all items of the typefile
, in current folder and subfolderscut -d/ -f2
to cut out their specific foldersort
to sort the list of foldernamesuniq -c
to return the number of times each foldername has been counted
This prints the file count per directory for the current directory level:
du -a | cut -d/ -f2 | sort | uniq -c | sort -nr