Find directory with biggest number of files / directories
Today we had a problem related with a number of files in a directory. We needed to find directories with a biggest number of files / directories in it. Here is a small shell script that will list directories and a number of files/directories in each directory.
find . -type d|awk {‘print “echo -n “$1″.\”\t\”; ls “$1″|wc -l”‘} > /tmp/do; . /tmp/do|sort -k 2 -n -r |more
Explaining it a little bit:
“find . -type d” – find all directories bellow current directory
“awk {‘print “echo -n “$1″.\”\t\”; ls “$1″|wc -l”‘} > /tmp/do” – generate a script that will print a directory name (echo -n), make ls in this directory and count a number of lines from ls (wc -l)
“. /tmp/do” – execute generated script
“sort -k 2 -n -r” – sort the result using second column (-k 2) as numerical (-n) and in reverse order (-r)
Recent Comments