LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Finding Directory Size (https://www.linuxquestions.org/questions/linux-software-2/finding-directory-size-840457/)

Manjunath1847 10-26-2010 01:23 AM

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.

neonsignal 10-26-2010 04:55 AM

Quote:

Originally Posted by Manjunath1847 (Post 4139370)
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.

gdejonge 10-26-2010 04:58 AM

Try du command:
Code:

gerrard@orion:~$ du -sh Documents
251M    Documents

Read the fine man page about the different options for the du command.

Manjunath1847 10-26-2010 10:18 PM

I want to find the size from the C program..Not from command line

Manjunath1847 10-26-2010 10:22 PM

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 (Post 4139545)
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.


neonsignal 10-27-2010 04:54 AM

Quote:

Originally Posted by Manjunath1847 (Post 4140424)
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.


All times are GMT -5. The time now is 10:00 PM.