我试图在shell脚本的某些文件夹中生成用户使用情况,并且使用find和du组合发现一些奇怪的行为。
我有一个约9500个文件的文件夹,总计5GB。此文件夹的4GB由7个大文件组成,其余1GB由小文件(近9000个文件)组成。我遇到的问题是我的脚本似乎忽略了大部分文件,因此du报告的总使用量不正确。
下面的行为文件总数提供了正确的数字(总数为+1):
$ find . -type -f -exec du -ch {} + | wc -l
9596
但是,如果我只是尝试获取所有文件的总数,它将返回错误值。
$ find . -type -f -exec du -ch {} +
...lines of files
139M total < this value is incorrect, should be ~5GB
如果我将大小限制为大文件(超过25MB),它的确会拾取大文件并使其靠近,但显然缺少组成剩余1GB的许多小文件。
$ find . -type -f -size +25M -exec du -ch {} +
561M ./largefile0
483M ./largefile1
514M ./largefile2
948M ./largefile3
360M ./largefile4
768M ./largefile5
764M ./largefile6
4.3G total < this is the correct total for these files
最后,甚至更陌生的是,如果我再次运行不带size参数的命令,然后grep为其large
选择largefile[0-6]
文件。
$ find . -type -f -size +25M -exec du -ch {} + | grep large
561M ./largefile0
483M ./largefile1
514M ./largefile2
948M ./largefile3
360M ./largefile4
768M ./largefile5
764M ./largefile6
所以我不太确定这里发生了什么,好像du可以报告的文件总数有一个限制,就是它的总数,但是对于这个数据集,看起来总是等于139M。
不清楚你在问什么。如果仅是结果的说明,则:
该-exec {} +
谓词不执行只有一次,它有一个大小的限制,当命令被执行,一个新的“命令”开始组成看到的局面:https://git.savannah.gnu.org/cgit/findutils。 git / tree / find / exec.c#n153
在我的机器上,大小约为〜128K。
$ find ./ -type f -name 'file*' -exec echo {} + 2>/dev/null | head -1 | wc -c
131056
对于包含约40000个文件的示例目录,该命令将执行7次。
$ find ./ -type f -name 'file*' -exec echo {} + | wc -l
7
如果我限制文件的数量,则它适合一个:
$ find ./ -type f -name 'file5*' -exec echo {} + | wc -l
1
如果要最终获得文件总大小:
find ./ -type f -name 'file*' -exec du -ck {} + | awk ' $2 == "total" { T+=$1 } END { print T } '
请注意,我使用k
而不是h
确保所有结果都具有相同的数量级。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句