Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of a directory [duplicate]

Tags:

Is there a way to get the directory size/folder size without actually traversing this directory and adding size of each file in it? Ideally would like to use some library like boost but win api would be ok too.

like image 461
smallB Avatar asked Apr 04 '12 16:04

smallB


People also ask

What is the size of a directory?

When listing the contents of a directory using the ls command, you may have noticed that the size of the directories is almost always 4096 bytes (4 KB). That's the size of space on the disk that is used to store the meta-information for the directory, not what it contains.

Why is size on disk double?

It is because of the large number of small files and cluster size. Mostly the size on disk is much greater than the actual file size. Moreover, in some exceptions, this size can be less than the actual file size if your operating system uses the auto compression system.

How much space does a directory take?

Assuming you use all 26 characters, with 1 billion unique names you can have about 7 character length folder names. So, 7 Bytes * 1.000. 000.000 equals roughly 7GB of space.

How do I check the size of a directory in Linux?

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.


2 Answers

As far as I am aware you have to do this with iteration on most operating systems.

You could take a look at boost.filesystem, this library has a recursive_directory_iterator, it will iterate though ever file on the system getting accumulation the size.

http://www.boost.org/doc/libs/1_49_0/libs/filesystem/v3/doc/reference.html#Class-recursive_directory_iterator

include <boost/filesystem.hpp>
int main()
{
    namespace bf=boost::filesystem;
    size_t size=0;
    for(bf::recursive_directory_iterator it("path");
        it!=bf::recursive_directory_iterator();
        ++it)
    {   
        if(!bf::is_directory(*it))
            size+=bf::file_size(*it);
    }   
}

PS: you can make this a lot cleaner by using std::accumulate and a lambda I just CBA

like image 144
111111 Avatar answered Oct 08 '22 06:10

111111


I don't think there is something like that, at least no win32 api function.

Natively for windows:

void DirectoryInfo::CalculateSize(std::string _path)
{
    WIN32_FIND_DATAA data;
    HANDLE sh = NULL;

    sh = FindFirstFileA((_path+"\\*").c_str(), &data);

    if (sh == INVALID_HANDLE_VALUE )
    {
            return;
    }

    do
    {
        // skip current and parent
        if (std::string(data.cFileName).compare(".") != 0 && std::string(data.cFileName).compare("..") != 0)
        {

            // if found object is ...
            if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
            {
                // directory, then search it recursievly
                this->CalculateSize(_path+"\\"+data.cFileName);


            } else
            {
                // otherwise get object size and add it to directory size
                this->dirSize += (__int64) (data.nFileSizeHigh * (MAXDWORD ) + data.nFileSizeLow);
            }
        }

    } while (FindNextFileA(sh, &data)); // do

    FindClose(sh);

} 
like image 45
Zilog Avatar answered Oct 08 '22 06:10

Zilog