12

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.

1
  • 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, 2014 at 22:11

4 Answers 4

17

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'
6
  • @X Tian see my comment on glenn jackman's answer Feb 10, 2014 at 22:18
  • Accepted, that is why I use -m and sort
    – X Tian
    Feb 10, 2014 at 22:50
  • 3
    Remember to shopt -s dotglob, otherwise du -sm Directory/* will not count hidden files/directories. Oct 7, 2015 at 8:56
  • 1
    I got it from here
    – Maveňツ
    Jan 19, 2017 at 11:36
  • 1
    @Maveňツ thanks for that reference, I'd never notice the -h on sort, before ! :-)
    – X Tian
    Jun 28, 2020 at 21:19
7
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    .
2
  • 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. Feb 10, 2014 at 22:17
  • There is df but that works on the whole filesystem. Feb 10, 2014 at 22:18
1

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.

0

It doesn't exactly fit your needs (not one-line print) but its way easier than others solutions :

du -a -h | sort -hr | head -2

This command will give you the size of the directory and sub-directory and the size of the bigest file or sub-directory on the current directory.

PS: The head -2 is in case a sub-directory take all the place of the current directory and is listen before the current.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .