Skip to content
Advertisement

In Linux, how do I find find directory with the most subdirectories or files? [closed]

How can I find the directory with the largest number of files/subdirectories in it on the system? Obviously the clever answer is /, but that’s not what I’m looking for.

I’ve been told the filesystem is out of nodes, so I suspect that somewhere there are a lot of files/directories which are just garbage, and I want to find them.

I’ve tried running this:

$ find /home/user -type d -print | wc -l

to find specific directories.

Advertisement

Answer

starting from the current directory, you could try

find . -type d | cut -d/ -f 2 | uniq -c

This will list all directories starting from the current one, split each line by the character “/”, select field number “2” (each line starts with “./”, so your first field would be “.”) and then only outputs unique lines, and a count how often this unique line appears (-c parameter).

You could also add an “sort -g” at the end.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement