Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a way to quickly get an accurate size for a directory (including, recursively, all its sub-directories)? I don't want the sizes of the subdirs, I just mean that they should be recursively included in the total size reported.

Failing that, a way to get an approximate size? I am thinking something along the lines of df that produces a quick response for the entire file system, but this time for a specific directory (and it's subdirectories).

Solutions using du seem to take ages when given a directory contains thousands of sub-dirs.

share|improve this question
    
Using du is the only method that I'm aware of. The problem is that the data is spread throughout the hierarchy so it needs to be tallied by a tool to get the counts. –  slm Feb 10 at 22:11
add comment

3 Answers 3

I find this is quite useful.

du -sm Directory

or to get a breakdown of inside directories

du -sm Directory/*

and then with a sort if it's got many subdirs

du -sm Directory/* | sort -n 

The reason I use the -m option is to ensure I get a megabyte output. I find this is easier for me to compare visually (when I'm not getting a mixture of units eg compare 999 KB and 1MB vs 999000MB and 1000000MB), the added benefit is that you can pass output into sort.

Put it all in the background and redirect output, it's going to take time what ever method one uses, it's a traverse of many files which will take time.

du -sm Directory/* | sort -nr > ~/cacheDu.log &

Note out the reverse sort so the big ones are at the beginning. Add this to cron run every n mins.

Then with an alias ducache, you've got a fairly up-to-date directory usage.

export alias ducache='head -n 15 ~/cacheDu.log'
share|improve this answer
    
@X Tian see my comment on glenn jackman's answer –  Marcus Junius Brutus Feb 10 at 22:18
    
Accepted, that is why I use -m and sort –  X Tian Feb 10 at 22:50
add comment
du -sh "$dir"

will give you a summary of the total size in "human" formatted output.


Since you were looking for an alternative to du, I tried find, but it's exactly the same:

$ find . -printf "%s\n" | { 
    sum=0
    while read size; do ((sum+=size)); done
    echo $sum
}
122865202884
$ du -sb .
122865202884    .
share|improve this answer
    
Thanks, I'm aware of that but du takes way too long since it produces an accurate result. I was hoping for a tool / command that provides a quick n' dirty approximate answer. –  Marcus Junius Brutus Feb 10 at 22:17
    
There is df but that works on the whole filesystem. –  glenn jackman Feb 10 at 22:18
add comment

You may not like my answer, but I think the best way of doing what you want is to run the "du -s" in the background on a regular basis using cron and store the result in a file. Then when you want an approximate value, use the value from the file. You get to decide how often to run the du. I don't see a faster way of getting the value. The command du is efficient for what it does.

The only other way you could do this very quickly is to create a filesystem for the directory, then df will get you the answer you want very quickly.

If it is faster to know everything in the file system except your directory, you could run df for the file system, du for everything other than your directory and subtract.

share|improve this answer
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.