发现:文件大小限制相等但不同的单位返回不同的结果

发现:文件大小限制相等但不同的单位返回不同的结果

我注意到这两个列出 5 GiB 以下文件的命令产生了不同的结果:

find . -type f -size -5368709120c
find . -type f -size -5G

具体来说,使用千字节单位(5368709120c)的函数返回的附加文件大小大于使用 GiB 单位(5G)的函数返回的最大文件大小。

我从find手册页中读到以下内容:

-size n[cwbkMG]
          File uses n units of space.  The following suffixes can be used:
          `b'    for 512-byte blocks (this is the default if no suffix is used)
          `c'    for bytes
          `w'    for two-byte words
          `k'    for Kilobytes (units of 1024 bytes)
          `M'    for Megabytes (units of 1048576 bytes)
          `G'    for Gigabytes (units of 1073741824 bytes)

The size does not count indirect blocks, but it does count blocks 
in sparse files that are not actually allocated.  Bear in mind that the `%k'
and `%b' format specifiers of -printf handle sparse files differently.   The 
`b'  suffix always denotes 512-byte blocks and never 1 Kilobyte blocks, 
which is different to the behaviour of -ls.

因此,假设的单位G是 1073741824,则5G应该是5368709120c。问题是由于如何计算稀疏块或间接块造成的?

先谢谢您的帮助。

答案1

在我的 Kubuntu 中man find[强调我的]:

-size n[cwbkMG]
[…]
+前缀-通常表示大于和小于。请记住大小四舍五入到下一个单位。因此-size -1M不等同于-size -1048576c。前者只匹配空文件,后者匹配从 0 到 1,048,575 字节的文件。

因此,如果某个文件占用的空间大于 4GiB 且小于(或正好)5GiB,则它将符合 的条件-size 5G。您的文件将-size -5G无法匹配。

相关内容