November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930  

Find directory with biggest number of files / directories

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)

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>