Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the size of a folder?

I'm creating a folder to cache images inside Documents with my iPhone App. I want to be able to keep the size of this folder down to 1MB, so I need to to check the size in bytes of my folder.

I have code to calculate the size of file, but I need the size of the folder.

What would be the best way to do this?

like image 934
joseph_carney Avatar asked Feb 02 '10 23:02

joseph_carney


People also ask

How do you determine size of folder?

Go to Windows Explorer and right-click on the file, folder or drive that you're investigating. From the menu that appears, go to Properties. This will show you the total file/drive size. A folder will show you the size in writing, a drive will show you a pie chart to make it easier to see.

How can I tell the size of a folder in GB?

How to view the file size of a directory. To view the file size of a directory pass the -s option to the du command followed by the folder. This will print a grand total size for the folder to standard output. Along with the -h option a human readable format is possible.

How do I see the size of multiple folders?

One of the easiest ways is by holding the right-click button of your mouse, then drag it across the folder you want to check the total size of. Once you are done highlighting the folders, you will need to hold the Ctrl button, and then right-click to see Properties.


1 Answers

tl;dr

All the other answers are off :)

Problem

I'd like to add my two cents to this old question as there seem to be many answers that are all very similar but yield results that are in some cases very unprecise.

To understand why we first have to define what the size of a folder is. In my understanding (and probably the one of the OP) it is the amount of bytes that the directory including all of its contents uses on the volume. Or, put in another way:

It is the space becoming available if the directory would be completely removed.

I'm aware that this definition is not the only valid way to interpret the question but I do think it's what most use cases boil down to.

Error

The existing answers all take a very simple approach: Traverse the directory contents, adding up the sizes of (regular) files. This does not take a couple of subtleties into account.

  • The space used on the volume increments in blocks, not in bytes. Even a one byte file uses at least one block.
  • Files carry around meta data (like any number of extended attributes). This data must go somewhere.
  • HFS deploys file system compression to actually store the file using less bytes then its real length.

Solution

All of these reasons make the existing answers produce unprecise results. So I'm proposing this extension on NSFileManager (code on github due to length: Swift 4, Objective C) to remedy the problem. It's also quite a bit faster, especially with directories containing a lot of files.

The core of the solution is to use NSURL's NSURLTotalFileAllocatedSizeKey or NSURLFileAllocatedSizeKey properies to retrieve file sizes.

Test

I've also set up a simple iOS test project, demonstrating the differences between the solutions. It shows how utterly wrong the results can be in some scenarios.

In the test I create a directory containing 100 small files (ranging from 0 to 800 bytes). The folderSize: method copied from some other answer calculates a total of 21 kB while my allocatedSize method yields 401 kB.

Proof

I made sure that the results of allocatedSize are closer to the correct value by calculating the difference of the available bytes on the volume before and after deleting the test directory. In my tests the difference was always exactly equal to the result of allocatedSize.

Please see Rob Napier's comment to understand that there's still room for improvement.

Performance

But there's another advantage: When calculating the size of a directory with 1000 files, on my iPhone 6 the folderSize: method takes about 250 ms while allocatedSize traverses the same hierarchy in 35 ms.

This is probably due to using NSFileManager's new(ish) enumeratorAtURL:includingPropertiesForKeys:options:errorHandler: API to traverse the hierachy. This method let's you specify prefetched properties for the items to be iterated, resulting in less io.

Results

Test `folderSize` (100 test files)     size: 21 KB (21.368 bytes)     time: 0.055 s     actual bytes: 401 KB (401.408 bytes)  Test `allocatedSize` (100 test files)     size: 401 KB (401.408 bytes)     time: 0.048 s     actual bytes: 401 KB (401.408 bytes)  Test `folderSize` (1000 test files)     size: 2 MB (2.013.068 bytes)     time: 0.263 s     actual bytes: 4,1 MB (4.087.808 bytes)  Test `allocatedSize` (1000 test files)     size: 4,1 MB (4.087.808 bytes)     time: 0.034 s     actual bytes: 4,1 MB (4.087.808 bytes) 
like image 79
Nikolai Ruhe Avatar answered Oct 11 '22 10:10

Nikolai Ruhe