0

I am trying to find a directory having highest number of files inside it. I am aware that I can find the number of files using:

find -maxdepth 5 -type f | wc -l 

but this is only of use when I know which directory to check. I want to find that directory containing highest number of files.

legoscia
  • 39,593
  • 22
  • 116
  • 167
  • [Possible Duplicate] http://stackoverflow.com/questions/15204980/how-to-find-subdirectory-of-some-directory-which-have-most-files – NaN May 13 '15 at 09:56
  • Isn't the directory with the most files always at the top level? So just run all the top level directories through `find -type f | wc -l` and sort? – Reinstate Monica Please May 13 '15 at 18:01

1 Answers1

1

You can create a list with directory names and the number of files they contain using the following nested find command:

find -maxdepth 5 -type d \
  -exec bash -c 'n=$(find {} -maxdepth 1 -type f -printf x | wc -c); echo "{} $n"' \

If you pipe that to:

find ... | sort -k2n | tail -n1

you'll get the directory which contains the most files.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266