LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-26-2010, 01:23 AM   #1
Manjunath1847
Member
 
Registered: Mar 2007
Posts: 44

Rep: Reputation: 16
Finding Directory Size


Is there any Linux function to find the size of the directory? stat() function can be used only for files and not directory. Can anyone let me know for to find the size of the directory from the C code.

Thanks in advance.
 
Old 10-26-2010, 04:55 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by Manjunath1847 View Post
stat() function can be used only for files and not directory.
The stat function can be used for directories too, and will provide information about the directory node.

Quote:
Can anyone let me know for to find the size of the directory from the C code.
However, I suspect you mean the total size of the contents of the directory rather than the directory itself.

No, there is not an API function that will do this for you. Typically you would recurse through the file tree below the directory, perhaps using readdir to work through the entries, and totalling up the sizes of each of the files.

Last edited by neonsignal; 10-26-2010 at 04:56 AM.
 
Old 10-26-2010, 04:58 AM   #3
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
Try du command:
Code:
gerrard@orion:~$ du -sh Documents
251M    Documents
Read the fine man page about the different options for the du command.
 
Old 10-26-2010, 10:18 PM   #4
Manjunath1847
Member
 
Registered: Mar 2007
Posts: 44

Original Poster
Rep: Reputation: 16
I want to find the size from the C program..Not from command line
 
Old 10-26-2010, 10:22 PM   #5
Manjunath1847
Member
 
Registered: Mar 2007
Posts: 44

Original Poster
Rep: Reputation: 16
Ya. I want to find total size of the contents of the directory rather than the directory itself. stat() on each file recursively looks good. My requirement whenever a file is added to the directory, I need to find the size of the directory, if it is greater than some size I need delete the older most files. Since process of adding new files and purging old files happens very frequently I am afraid if the calling stat() on each file would cause performance issue.

Quote:
Originally Posted by neonsignal View Post
The stat function can be used for directories too, and will provide information about the directory node.



However, I suspect you mean the total size of the contents of the directory rather than the directory itself.

No, there is not an API function that will do this for you. Typically you would recurse through the file tree below the directory, perhaps using readdir to work through the entries, and totalling up the sizes of each of the files.
 
Old 10-27-2010, 04:54 AM   #6
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by Manjunath1847 View Post
Since process of adding new files and purging old files happens very frequently I am afraid if the calling stat() on each file would cause performance issue.
It will depend on how often you do it. If you are running it all the time, you will find that the directory entries will get cached and it will run relatively fast after the first scan is done. But yes, it will be wasting some resources.

If your application is a continuously running process (ie, a daemon), then it could make use of the inotify interface. In other words, after an initial scan, it would incrementally keep track of the changes (such as total size) as files within the hierarchy change and the changes are reported back.
 
  


Reply



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
Finding a file size LneWlf Linux - Newbie 6 12-07-2009 06:06 PM
Finding File Size cmfarley19 Linux - General 1 01-24-2005 08:36 AM
finding size of total files in a directory blackzone Linux - Newbie 3 01-07-2005 03:01 AM
Finding files via their size ivanatora Linux - General 1 09-08-2004 08:23 AM
Finding size of a directory chrisk5527 Linux - General 2 12-30-2003 08:49 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration