Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.