如何获取文件夹的大小,包括稀疏文件的表观大小? (du太慢了) [英] how to get size of folder including apparent size of sparse files? (du is too slow)

查看:604
本文介绍了如何获取文件夹的大小,包括稀疏文件的表观大小? (du太慢了)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多KVM qcow2文件的文件夹,它们都是稀疏文件. 现在,我需要获取文件夹的总大小,应将qcow2文件大小计为表观大小(而不是实际大小).

I have a folder containing a lot of KVM qcow2 files, they are all sparse files. Now I need to get the total size of folder, the qcow2 file size should be counted as apparent size(not real size).

例如:

图片:c9f38caf104b4d338cc1bbdd640dca89.qcow2 档案格式:qcow2 虚拟大小:100G(107374182400字节) 磁盘大小:3.3M cluster_size:65536

image: c9f38caf104b4d338cc1bbdd640dca89.qcow2 file format: qcow2 virtual size: 100G (107374182400 bytes) disk size: 3.3M cluster_size: 65536

图片应被视为100G,而不应视为3.3M

the image should be treated as 100G but not 3.3M

最初我使用statvfs(),但它只能返回文件夹的实际大小.然后我切换到'du --apparent-size',但是考虑到我有10000+个文件,这太慢了,要花将近5分钟的时间.

originally I use statvfs() but it can only return real size of the folder. then I switch to 'du --apparent-size', but it's too slow given I have 10000+ files and it takes almost 5 minutes to caculate.

有人知道一种快速的方法,可以计算出qcow2的虚拟大小的文件夹大小吗?谢谢

anybody knows a fast way that can get the size of folder counting qcow2's virtual size? thank you

推荐答案

如果不stat()查找目录中的每个文件,就无法找到此信息.如果在单个目录中有这么多文件,速度会很慢. stat()需要检索每个文件的索引节点.

There is no way to find out this information without stat()ing every file in the directory. It is slow if you have this many files in a single directory. stat() needs to retrieve the inode of every single file.

添加更多内存可能有助于缓存.

Adding more memory might help due to caching.

这篇关于如何获取文件夹的大小,包括稀疏文件的表观大小? (du太慢了)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆