LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
Search this Thread
Old 04-20-2005, 07:55 AM   #1
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Rep: Reputation: 15
stat directory


hi ,

stat() call is used find size of a file. but i need a function call returns size of directory,--( including its files , sub directory, subdirectory's files & subdirectories files ........)


i.e; suppose if i asked for directory size , it should display size of its contents below that directory)

Example :

Here xyz is directory

sizefucntion( /tmp/xyz )

If xyz contails following

xyz
|------>tmp.c
|------> project [ This is subdirectory ]
| |--------------------> file1
| |--------------------> file2
| |--------------------> file3
| |--------------------> database [ This is subdirectory ]
| |--------------------> emp.db
| |--------------------> test.db
|
|-------> results.c


in above function should return size of all files including subdirectory files.

reply me soon

Thanks in advance
 
Old 04-20-2005, 01:12 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
I'm not sure if you are looking for something in a specific language but you can use df:
df -s /path/to/dir

If you are looking for code then check it's source.
 
Old 04-20-2005, 11:40 PM   #3
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
i'm asking for c (linux) function.
 
Old 04-21-2005, 07:19 AM   #4
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
may need to roll your own function ,
you do a recursive scan directory and add up the file size for every file found in a directory

you may use functions like :

opendir
readdir
stat or statfs ?
closedir

check man for info ,
search for df or ls source file for examples code .

may have other more effecient way or functions for these though ,

hope it help
good luck
 
Old 04-21-2005, 07:37 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Yes. There no readily available standard function that does this in one go. There may be some non-standard tools-library that provides this out there, but I haven't herad of any.

Using standard libary functions, you'll indeed need to roll your own, recursing into the subdirs, stat()-ing all files and add them all up.

Beware of hard links: do you want to double-count them? Probably not. The GNU "du" utility prevents double-counting hardlinks by storing and checking all visited inodes in a hash table.

If you intent to GPL your program, you could have a look at the sources of "du", or just copy parts of it right away (crediting GNU properly). You can find "du" in GNU's coreutils package.

Last edited by Hko; 04-21-2005 at 07:39 AM.
 
Old 04-22-2005, 01:40 AM   #6
minil
Member
 
Registered: Dec 2004
Location: Bangalore, India
Distribution: Fedora 2
Posts: 74

Original Poster
Rep: Reputation: 15
I am writing program to open directory & read its contents & stat for each file.
& sum the filesize
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot stat /etc/X11/X (no such file or directory) theabyyss Linux - Software 2 02-12-2008 08:15 PM
Use of stat() in C rblampain Programming 3 06-02-2005 07:41 AM
error: stat of /var/log/ppp/connect-errors failed: No such file or directory Niceman2005 Linux - Newbie 0 01-20-2005 01:29 AM
Linux stat to Windows Stat sridurai Programming 3 09-24-2004 05:07 PM
lsattr: no such file or directory while trying to stat... acampbell Linux - General 6 12-01-2003 03:41 PM


All times are GMT -5. The time now is 02:41 AM.

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