LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-07-2003, 02:56 PM   #1
linuxSysProg
LQ Newbie
 
Registered: Aug 2003
Posts: 2

Rep: Reputation: 0
Question How Can I get the Total Size of a Dir w/o Recursing Into it?


Hi Everybody,
I'm wiriting a program in (C++) that will display the size of each directory... and I don't mean 4096, but the size of the directory and all that is in it. The issue I'm having is that "ls -s" will only give you that 4096 byte size of a directory, and the only way I've seen on getting the actual size is to actually traverse into the directory and add up the size of every single file... well this can take forever if you are trying to determine the size of a directory that may contain a number of sub-directories with many many files. Is there some kind of system call that would actually display the true amount of space that a directory takes up?

I've been using the statfs, and dirent structs to get file sizes and traverse into directories... I would really appreciate it if someone knows of a more efficient way.

Thanks.
 
Old 08-07-2003, 03:51 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Doing that is inherently not efficient as the program will have to traverse all subdir's and read all the file sizes in them. Smart programming is the only way to improve performance. You mention statfs() but I think stat() will be more useful.

Anyways, there already is the "du" utility that has quite a few useful options. You may want to read it's man page to see if it scratches your itch. I think it will, but if it doen't, or if you just want to write your own for fun or for learning, a look at the sources of "du" may be enlightning.
 
Old 08-07-2003, 04:41 PM   #3
linuxSysProg
LQ Newbie
 
Registered: Aug 2003
Posts: 2

Original Poster
Rep: Reputation: 0
Thank You,
Yeah, I wrote statfs, but actually I'm using stat in my code, I wasn't looking at the code at that moment... the stat struct has a value called st_size, that can give the size in bytes but the problem was that for a directory it wouldn't give a size total on the contents of the dir.
But du looks pretty good and I'll probably end up using it if I can find some kind of system call that I can run from within the program. Do you know of one?
 
Old 08-08-2003, 08:39 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Nope, I'm afraid there isn't any.
Off course you can take code from du (it's GPL'd), and give credits, where credits are due.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH : Total directory size xgreen Programming 7 03-04-2013 05:33 AM
how to get the total size of VCD with ioctl on CDROM al_periodical Programming 0 03-19-2005 11:26 AM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 03:01 AM
Total partition size - User partition size is not equals to Free partition size navaneethanj Linux - General 5 06-14-2004 12:55 PM
Total File Size meshcurrent Linux - General 1 05-17-2004 02:09 PM


All times are GMT -5. The time now is 08:22 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration