Find 无法找到 1M 以下的文件,但可以正常使用 'k' 个单位

Find fails to find files below 1M but works fine with 'k' units

我创建了名为 1.txt 的文件,大小为 9kb:

stat 1.txt
  File: `1.txt'
  Size: 9322            Blocks: 24         IO Block: 4096   regular file
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2015-06-26 10:12:47.000000000 +0100
Modify: 2015-06-26 10:12:47.000000000 +0100
Change: 2015-06-26 10:12:47.000000000 +0100

当我 运行 find 命令带有 k 个单位的选项 -size -100k 时,找到文件:

#find . -type f -size -100k
#./1.txt

当我使用 M 单位并且仍然找到 -size -10M 文件时:

#find . -type f -size -10M
#./1.txt

但是当我尝试查找小于 1M 的文件时,突然 find 找不到这个文件:

#find . -type f -size -1M
#

是的,我的 find (find (GNU findutils) 4.4.0) 版本支持 M 单元。

我认为这是因为单位表示为整数,而 "less than 1" 为 0,因此它搜索最大为 0megs 的文件。

肯定与直觉相反,但可能为了与遥远的过去向后兼容而保留。

大小四舍五入为 space 的整数单位。在你的 例如,

find . -type f -size -10M

表示文件大小<=9M,

find . -type f -size -1M

表示,文件大小<=1M, 这实际上意味着 0M,因为小数单位(例如 0.5M)是 不支持。

以下内容摘自 gnu find 手册:

-size n[cwbkMG]
     File uses n units of space, rounding up.  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.

     The  + and  - prefixes  signify greater  than and  less
     than, as usual.  Bear in  mind that the size is rounded
     up  to  the  next  unit. Therefore  -size  -1M  is  not
     equivalent to -size -1048576c.  The former only matches
     empty  files,  the  latter  matches  files  from  1  to
     1,048,575 bytes.