unix du 命令使用什么算法来计算磁盘空间?
我尝试使用 du 来计算今天单个目录的磁盘使用情况。目录大小为 6GB,计算时间如下:
prompt> time du -sh .
6.0G .
real 1m32.405s
user 0m2.053s
sys 0m16.552s
unix du 命令使用什么算法来计算磁盘空间?为什么这么慢?有没有其他方法可以更有效地计算磁盘空间?调用平台是Sun
。计算大小的目录是 NFS 挂载的。
I've tried using du
to calculate disk usage today on a single directory. The size of directory is 6GB and it took the following amount of time to calculate:
prompt> time du -sh .
6.0G .
real 1m32.405s
user 0m2.053s
sys 0m16.552s
What is the algorithm unix du command uses to calculate disk space? Why is it so slow? Are there any alternatives to calculate disk space more efficiently? The platform of invocation is Sun
. The directory for which size was calculated is NFS mounted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
du 必须列出所有目录并统计找到的每个文件,以便最终读取整个磁盘
,同时获取文件的大小,并将它们相加,完成后,它会打印总和,
例如在这个包含大约 200 万个文件的目录上在 sshfs 文件系统上:
但由于第二次运行的缓存,它只需要:
du has to list all the directories and stat every file found so that ends up with read all over the disk
while getting the sizes of the files it sums them and when finished it prints the sum
for example on this one directory with like 2 million files on a sshfs filesystem:
but due to caching for the second run it only takes:
我认为它只是递归地遍历目录,同时将找到的文件的大小加在一起。确实很简单,但当然需要时间。如果文件系统使这些操作更快,那么速度可能会更快。
I think it just recursively walks the directory, while adding together the sizes of the files found. Quite simple, really, but of course it takes time. It might be faster if the file system makes these operations quicker.
它的速度取决于文件/目录的数量。如果您有一个包含 6 个 1Gb 文件的目录,则计算所需的时间会少得多。它计算给定目录中的文件大小,并为每个子目录递归计算。
Its speed depends on files/directories count. If you have a directory with 6 1Gb files, it will take much less time to calculate. It calculates files size in the given directory, and recursively for each child directory.