However, I was asking myself if there is a faster way to get the size of a folder?
The answer may depend on the file system in use. Like most operating systems, macOS supports a variety of different file systems, and the best way to determine the size of a directory may not be the same for all file systems. Accordingly, I'll answer the remaining questions with Apple's file system, APFS, in mind.
Enumerating over all files can take a very long time and when I use the Finder and right-click a folder and select "Information", the size is displayed instantly.
Just because the information seems to be displayed instantly doesn't mean that Finder isn't calculating the sum of the file sizes in the directory. Indeed, if you try the Get Info command on a directory known to have a great many files, such as /System
, you'll see that Finder takes considerable time to determine the size of the folder:
Is the size a "secret" property of a folder that is only available to the OS? Does the operating system index all folders/files in the background?
It certainly doesn't seem to. Again, try looking at a directory that contains thousands of files in a variety of subdirectories.
Also, try using the du
command to determine the disk usage of a large directory, e.g. du -d0 somedirectory
; it responds almost immediately for directories with dozens of files, but can take many seconds for large directories.
When I do the same thing in Windows, the subfolders and files are also enumerated.
Consider that if the file system were to constantly keep track of the sizes of all directories, writing to any file could change the size of the file, as well as every directory in the file's path. Since asking for the size of a directory happens much, much less often than writing to a file (or moving or deleting a file), the file system would be doing a lot of work for very little benefit.
Consider also that there's more than one way to ask about directory size. You might want to know how much space is used by the files in a directory, but exclude subdirectories. You might want to know about logical file size (the number of bytes in the file) or physical size (the number of blocks used to store those bytes). You might want to include symbolic links in the calculation, or not. Being prepared to answer just one version of the question probably isn't all that helpful, and being prepared to answer every version of the question would require a lot of work.
With all that in mind, it's not surprising that both macOS and Windows calculate directory sizes when asked by recursively enumerating all the files and subdirectories in the directory.
If you want an absolutely authoritative answer for the file system(s) that you're working with, look at the published information for that system. For example, here's the data for Apple File System (ApFS)(PDF). After a quick look, I don't see fields for the cumulative sizes of directories in the relevant structures.
Note that if you select an entire volume in MacOS and use the Get Info command, you do see the amount of space used almost instantly. I believe this is because the file system has to keep track of the volume's total size and free space because in order to be able to provide free blocks as they're needed. Determining the amount of space used is therefore very simple.