tags:

views:

127

answers:

6
+2  Q: 

Folder size linux

Hi

I need to monitor folders on my linux machine periodically to check whether they are exceeding certain limits.

I have checked stat function call but running stat recursively on all sub folders and files is time consuming and I need to do this for all folders.

Does kernal maintain any datastructures which I can interpret in my program.Or is their any standard api for this.

+3  A: 

If you need to enforce limits then use quotas

Axel Gneiting
+1  A: 

You want quotactl:

quotactl -- manipulate filesystem quotas

SYNOPSIS
     #include <sys/types.h>   /* types needed by quota.h */
     #include <sys/quota.h>   /* for disk quotas */
ennuikiller
+2  A: 

In case the quota mechanism isn't suitible, then inotify might be handy:

From wikipedia:

inotify is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications.report those changes to applications.

Tom Feiner
@Tom I have checked inotify events, folder size monitoring is not present in this
siri
@siri Yes, I know, but you can hook to every IN_CLOSE_WRITE and perform some actions instead of doing it all the time without knowing if anything changed in the directory.
Tom Feiner
+1  A: 

Calling stat recursively is the only way to get the current folder size. If you want to continuously monitor the file system, take a look at inotify.

Karl Bielefeldt
Although, be careful which stat fields you use to figure out the amount of space used. You probably need to round the usage for each file or directory **up** to the next multiple of `st_blksize`
JeremyP
A: 

I agree with @Axel. However if you really need to do this in code, you could execute the du shell command via popen:

FILE* pf = popen("du -ch [your folder] | grep total")

Then read the output from the command via the file handle and fgets(...)

Adrian Regan
+1  A: 

Type

du -sm directory_name

Will give you the total size of the directory in megabytes recursively.

Type

man du

for help with this command.

Chance