开发者

Folder size linux

开发者 https://www.devze.com 2023-01-13 15:38 出处:网络
I need to monitor folders on my linux machine periodically to check whether they are exceeding certain limits.

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 su开发者_如何学Gob 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.


If you need to enforce limits then use quotas


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.


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.


You want quotactl:

quotactl -- manipulate filesystem quotas

SYNOPSIS
     #include <sys/types.h>   /* types needed by quota.h */
     #include <sys/quota.h>   /* for disk quotas */


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.


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


Use command du, check this link ... "How To Check Folder Size"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号