I have a fairly large directory tree whose contents I am not familiar with, and I have reason to believe that there are some enormously large directories buried in there somewhere. How do I find where they are?

Context: I am cleaning up a web application that has left behind a lot of files over the years. I already found and cleared a directory that contained hundreds of thousands of tiny ancient "session.*" files, but find commands over the hierarchy are still extremely slow.

The tree is large enough that ls -RC | less is not an attractive solution. If I was looking for a directory taking up a lot of disk space, I would divide and conquer using du -s *. How can I find the directories that contain the largest number of files?

Edit: This is on an old server, so I cannot use du --inodes to count the files. For most readers that's probably the best solution.

up vote 3 down vote accepted

On an old distribution without du --inodes,

find . -type d -ls | sort -n -k7,7

will list all directories in order of increasing size, so you'll get the directories with the most files (or which contained the most files at some point) at the end of the list. The find command might well take a while to run but there's not much that can be done about that — even a divide-and-conquer du approach would have to read the same amount of information from the disk.

  • Nice, thanks! I was just putting together a find -exec commandline that counts the number of files in each directory with wc, then sorted; yours is close enough, and much simpler :-). And I can use sort -rn to get the large directories first. – alexis Nov 28 '16 at 13:54
du --inodes *

man du:

   --inodes
          list inode usage information instead of block usage
  • Sounds great, but unfortunately my du has no --inodes option and no differently named option to count files. (This is on a rather old Debian distribution.) Any other ideas? – alexis Nov 28 '16 at 13:23
  • @alexis It would be better if that kind of caveats were in the question itself. Can you specify which Debian release you are on? – a CVn Nov 28 '16 at 13:41
  • @Michael, apologies -- I hadn't realized that there was a solution in other versions of du! – alexis Nov 28 '16 at 13:53

You can use

du --inodes * |sort -nr 

du --inodes * will give the number of files by directoryand sort -nr will sort this number in decreasing order

  • Sounds great, but unfortunately my du has no --inodes option and no differently named option to count files. (This is on a rather old Debian distribution.) Any other ideas? – alexis Nov 28 '16 at 13:23
  • du -bh |sort -nr |tail -n 20 – Dababi Nov 28 '16 at 13:37
  • this command will give you the top 20 directories or files ordered by disk space occupied – Dababi Nov 28 '16 at 13:39
  • That's a great suggestion, @Dababi. But I'd combine it with find . -type d in order to ignore regular files. – alexis Nov 28 '16 at 13:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.