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)
Monday, December 19, 2011
Find directory with biggest number of files / directories
Posted by Unknown at 10:07 PM 0 comments
Subscribe to:
Posts (Atom)