Linux 如何查找每个用户的磁盘空间使用情况?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9259912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:33:00  来源:igfitidea点击:

how to find per-user disk-space-usage?

clinuxunix

提问by Whoami

I am writing a small tool in which I require to find per-user File-system-memory-usage.

我正在编写一个小工具,我需要在其中查找每个用户的文件系统内存使用情况。

I have to do some clean up activity if file-system usage is crossing certain threshold value. What is the system call that I can use, so that I could be able to find per user memory usage?

如果文件系统使用率超过某个阈值,我必须进行一些清理活动。我可以使用什么系统调用,以便我能够找到每个用户的内存使用情况?

采纳答案by Basile Starynkevitch

Maybe you are interested in disk quotas(which are supported by some, but not all, filesystems). The low-level system call is quotactl(2). Perhaps using the existing tools quota(1)and quotacheck, edquota, warnquota, quotaonetc.... might be enough.

也许您对磁盘配额感兴趣(某些文件系统支持,但不是全部)。低级系统调用是quotactl(2)。也许使用现有工具的配额(1)quotacheckedquotawarnquotaquotaon等....可能是不够的。

If you want to know the memory used by a given process, consider the getrusage(2)syscall. You can also read pseudo-files under /proc/self/or /proc/1234for pid 1234. You can also scanthe /proc/(using usual directory scanning routines: opendir(3), looping on readdir(3)with stat(2), closing with closedir...) for numerical directories (since /proc/1234/describes the process of pid 1234). Read more about proc(5)(e.g. /proc/self/mapsor /proc/1234/smapsand /proc/1234/statusetc). Tou can query the virtual address space of process 1234 by reading the /proc/1234/statusand /proc/1234/mapspseudo-files. Try for example cat /proc/$$/statusand cat /proc/$$/mapsin a terminal to query info for your current shell process.

如果您想知道给定进程使用的内存,请考虑getrusage(2)系统调用。您还可以阅读伪文件下/proc/self//proc/1234进行PID 1234也可以扫描/proc/(通常使用扫描目录套路:执行opendir(3) ,循环上的readdir(3)统计(2) ,与关closedir...)的数值目录(因为/proc/1234/描述了pid 1234的过程)。了解更多关于PROC(5) (如/proc/self/maps/proc/1234/smaps/proc/1234/status等)。Tou 可以通过读取/proc/1234/status/proc/1234/maps伪文件来查询进程 1234 的虚拟地址空间。尝试例如cat /proc/$$/statuscat /proc/$$/maps在终端中查询当前 shell 进程的信息。

Of course, consider also the du(1)and df(1)commands (perhaps invoke them carefullythru popen(3)if you want to get their output). If for whatever reasons you want to recursively scan a file tree (e.g. to count its cumulated used size like dudoes), consider using the nftw(3)functions. See also stat(2)and statfs(2)syscalls.

当然,还要考虑du(1)df(1)命令(如果您想获得它们的输出,也许可以通过popen(3)仔细调用它们)。如果出于某种原因您想递归扫描文件树(例如,像计算它的累计使用大小一样),请考虑使用nftw(3)函数。另请参阅stat(2)statfs(2)系统调用。du

Remember that other processes can (and often do) write to the filesystem while your program is exploring or querying it. And your user could start new processes (perhaps indirectly, e.g. with crontab(5), system(3), fork(2)and execve(2), at, batch, ssh...) at any time.

请记住,当您的程序正在探索或查询文件系统时,其他进程可以(并且经常这样做)写入文件系统。并且您的用户可以随时启动新进程(可能是间接启动,例如使用crontab(5)system(3)fork(2)execve(2)atbatchssh...)。

Read also a good Linux programming book, perhaps the old Advanced Linux Programming, and syscalls(2)

也读一本很好的 Linux 编程书,也许是旧的Advanced Linux Programmingsyscalls(2)

回答by sehe

A simplistic approach would be

一个简单的方法是

du -shc /home/*

To sort it:

对其进行排序:

du -smc /home/* | sort -n

There is also a wellknown Perl script that has the option of mailing disk usage reports per user: durep

还有一个众所周知的 Perl 脚本,它可以选择按用户邮寄磁盘使用情况报告: durep