Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I misread this question and thought it was asking something I've needed for a long time. My question is slightly different: how to find directories whose contents exceed a certain size. Is there any *nix way to do this?

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

find doesn't look at the cumulative size of subdirectories. Instead, use du with the -s option to tell it to print only the total size of each directory given on the command line:

du -s *|awk '{ if ($1 > 3000) print }'

will give you output like:

3124    foo
10025   bar

or, to print just the names:

du -s *|awk '{ if ($1 > 3000) print $2 }'
share|improve this answer
    
Oh my, that is beautiful, thank you! –  Yar Feb 26 '10 at 15:39
add comment

Does find . -type d -size +3k -print do what you are asking? Change the "3k" to whatever size you want, of course.

share|improve this answer
    
@mpez0, I'm not sure. I'm trying it but I haven't decided how it's handling the subdirs yet. Ideally I'd like it to report only the top level directories whose recursive contents exceed the size. That way I can go down levels to investigate, or not. –  Yar Feb 26 '10 at 14:09
    
Recursive content? If you're looking for the size of the content of the directories, use the "du" (disk usage) command. Find will be looking at the actual size of the directory file itself –  mpez0 Feb 27 '10 at 18:25
add comment

A couple expansions on Peter Westlake's answer:

  • The --max-depth option can be used to limit the depth of subdirectories du will search. In the example below the search is to 2 levels.

  • You can use the -B option for du to set a block size for the du output. In the following example, the directory size is kilobytes (1000 bytes here). For now, you may be able to use the -k (kilobytes) and -m (megabytes) options, but man tells me they are deprecated, so I would recommend you avoid them. (Otherwise you'll be in the situation I may end up in - in the habit of using them and frustrated when they go away!)

    $ du --max-depth=2 -B 1000 | awk '{if($1>=5) print $0};'

One more point: If you want the output sorted by directory size, pipe it through sort -n, which will sort the output numerically (the -n) and default to using the first column (the directory size).

share|improve this answer
    
+1 for sort -n :) –  Yar Feb 26 '10 at 16:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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