Find the answer to your Linux question:
Results 1 to 6 of 6
Hey guys ! You know, I've been working hard the whole day trying to figure out one damn system-call or library function that could tell me the accurate size of ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Location
    India
    Posts
    30

    Find directory-size through C ??? LINUX ???

    Hey guys !

    You know, I've been working hard the whole day trying to figure out
    one damn system-call or library function that could tell me the
    accurate size of any directory (with its contents) anywhere in a
    filesystem.

    Believe me, I've pestered every book, web-page, and any reference
    on Linux programming that I could.
    And I now have an idea that there actually isn't any such library
    function or method (IN C/C++).

    Also, so far as I know, a directory's size is NOT simply a summation of its constituent files.

    So, can you please help me calculate the directory-size through a pseudo
    code or a ready-made C code
    (I'll kiss you for that! (on your head)).

    HELP !!!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Kisses or no, you can get the size of the directory itself (not including its constituent files) by calling stat() on the directory and multiplying together the st_blksize and st_blocks fields of the resultant struct stat.

    You can do exactly the same thing for each of the constituent files. If any of those is a directory, do the same thing for each of its constituent files, and so on.

    Then add it all up.

    As I'm sure you know by now, opendir(), readdir() (the one in section 3 of the man pages, not section 2), and closedir() are what you use to step through a directory. Skip the . and .. entries in each directory.

    I won't waste my time or insult your intelligence by actually writing the code, but that ought to be all you need.

    I just got a kiss from my wife (on the head), so that about wraps this up!

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Location
    India
    Posts
    30
    Thanks Bill !
    You're a nice pal.

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Not sure if this is what your looking for, but try:

    du -a directoryname

  5. #5
    Just Joined!
    Join Date
    Mar 2008
    Location
    India
    Posts
    30
    Quote Originally Posted by gerard4143 View Post
    Not sure if this is what your looking for, but try:

    du -a directoryname
    Thanks for your concern, Gerard,
    but I was actually looking for a method in C language,
    and not a command.
    But I think, if I look into the implementation of this 'du',
    I might find a way out.

    Thank you once again.

  6. #6
    Just Joined!
    Join Date
    Apr 2009
    Posts
    1

    Can you please help me with the script to find the size of the directory

    Hello,

    Well, i need to know the size of the directory and also the last modified date of the directory. So can u please help me with the script.

    It will be very helpful


    Thanx and Regards
    Abhishek.













    Quote Originally Posted by wje_lf View Post
    Kisses or no, you can get the size of the directory itself (not including its constituent files) by calling stat() on the directory and multiplying together the st_blksize and st_blocks fields of the resultant struct stat.

    You can do exactly the same thing for each of the constituent files. If any of those is a directory, do the same thing for each of its constituent files, and so on.

    Then add it all up.

    As I'm sure you know by now, opendir(), readdir() (the one in section 3 of the man pages, not section 2), and closedir() are what you use to step through a directory. Skip the . and .. entries in each directory.

    I won't waste my time or insult your intelligence by actually writing the code, but that ought to be all you need.

    I just got a kiss from my wife (on the head), so that about wraps this up!

    Hope this helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...