0

I'm looking for C good practice. I need to obtain a directory size in a C code on Linux. Please be aware that I'm really new to C.

I guess there are several ways to do this. Simpler might be to use "du" with a system() function and get the output. Correct?

More complicated would be to use stat() function recursively on all files and sub-directories, which is I guess what "du" already does.

I'm asking because I like to have the best performances possible. The size will require to be recalculated several times in my program process and as quickly as possible.

I also thought on "importing" the source code of "du" into my project if that is even an option.

Any input on this matter please?

trox
  • 306
  • 4
  • 12
  • 1
    The runtime of your program is bound by the speed of the file system. There should be little to no difference between invoking `du` and implementing this functionality yourself. – fuz Apr 12 '17 at 15:52
  • 2
    Almost exactly the same question here: http://stackoverflow.com/questions/472697/how-do-i-get-the-size-of-a-directory-in-c – Neil Apr 12 '17 at 15:59
  • Please explain exactly what you mean by "directory size". Your mention of recursive `stat` and of `du` suggests one of the several possibilities, but it's not quite precise enough to give a clear answer. – zwol Apr 12 '17 at 17:19

0 Answers0