user2809888
user2809888

Reputation:

how to find the directory/sub directory having the highest number of file

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.

Upvotes: 0

Views: 704

Answers (1)

hek2mgl
hek2mgl

Reputation: 158080

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.

Upvotes: 1

Related Questions