Thread: Is there a way to get a linux directory size in c++?

  1. #1
    Registered User
    Join Date
    Apr 2007

    Is there a way to get a linux directory size in c++?

    does linux provide such a function? Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2006
    du -sh dirname
    on the command line

    I don't think it's possible to do it directly in C++. You will probably have to write a recursive function to do it.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Quote Originally Posted by meili100 View Post
    does linux provide such a function? Thanks!
    Next door down...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Moved to Linux Programming.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    get all the files in the dir
    do stat() on each file
    accumulate the size
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    And use Boost.Filesystem's recursive_directory_iterator to do it. For example, if you're not worried about symlinks, you can do this:
    #include <boost/cstdint.hpp>
    #include <algorithm>
    #include <boost/filesystem.hpp>
    #include <boost/bind.hpp>
    #include <boost/iterators/transform_iterator.hpp>
    namespace fs = boost::filesystem;
    boost::uintmax_t dir_size(const fs::path &p)
      return std::accumulate(
        boost::make_transform_iterator(&fs::file_size, fs::recursive_directory_iterator(p)),
        boost::make_transform_iterator(&fs::file_size, fs::recursive_directory_iterator()),
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused about Memory
    By gL_nEwB in forum C++ Programming
    Replies: 22
    Last Post: 06-20-2006, 07:32 PM
  2. Linux Linux why Linux??
    By afreedboy in forum Tech Board
    Replies: 146
    Last Post: 01-21-2004, 06:27 PM
  3. Replies: 11
    Last Post: 03-25-2003, 05:13 PM
  4. Changing a Structures Members array size
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2002, 07:45 PM
  5. total size of a directory using C / C++
    By metthew in forum C Programming
    Replies: 1
    Last Post: 08-01-2002, 06:45 AM