为什么当我尝试查找 1 MB 或 1 GB 大小的文件时, find 会给出不准确/奇怪的响应? 甚至小于 1 MB 或 1 GB。
[root@server1 ~]# ls -lh
total 7.8M
-rw-------. 1 root root 1.7K Jan 24 00:15 anaconda-ks.cfg
-rw-r--r--. 1 root root 4.8M Mar 15 16:09 backup.tar
-rwxrw--w-. 1 root root 0 Mar 23 18:15 file
-rw-r--r--. 1 root root 9.6K Mar 11 19:26 grub.cfg
-rw-r--r--. 1 root root 88 Mar 17 20:10 local.repo
-rw-r--r--. 1 root root 512 Mar 14 22:33 mbr2.img
-rw-r--r--. 1 root root 512 Mar 14 22:30 mbr.img
-rwxr--r--. 1 root root 414 Mar 14 23:14 partition2.sh
-rwxr--r--. 1 root root 409 Mar 15 00:57 partition3.sh
-rwxr--r--. 1 root root 282 Mar 14 23:50 partition4.sh
-rwxr--r--. 1 root root 1.2K Mar 15 12:20 partition.sh
-rw-r--r--. 1 root root 1.2K Mar 23 03:03 testfile
-rw-r--r--. 1 root root 3.0M Apr 8 2020 zsh-5.0.2-34.el7_8.2.src.rpm
[root@server1 ~]#
[root@server1 ~]# find /root/ -size 1M |grep -v "/\."
/root/
/root/partition2.sh
/root/anaconda-ks.cfg
/root/partition.sh
/root/partition4.sh
/root/local.repo
/root/partition3.sh
/root/mbr.img
/root/testfile
/root/grub.cfg
/root/mbr2.img
[root@server1 ~]#
[root@server1 ~]# find /root/ -size 1G |grep -v "/\."
/root/
/root/partition2.sh
/root/anaconda-ks.cfg
/root/backup.tar
/root/partition.sh
/root/partition4.sh
/root/local.repo
/root/partition3.sh
/root/mbr.img
/root/testfile
/root/grub.cfg
/root/zsh-5.0.2-34.el7_8.2.src.rpm
/root/mbr2.img
[root@server1 ~]#
[root@server1 ~]# find /root/ -size -1M
/root/file
[root@server1 ~]# find /root/ -size -1G
/root/file
[root@server1 ~]#
我使用 grep 排除了隐藏文件。
答案1
文件大小为四舍五入到您指定的最接近的单位,即到您示例中的最接近的 1M 或 1G 单位。
find
引用 的测试选项-size
的文档https://man7.org/linux/man-pages/man1/find.1.html
-size n[cwbkMG] File uses less than, more than or exactly n units of space, rounding up. The following suffixes can be used:
[...]
The + and - prefixes signify greater than and less than, as usual; i.e., an exact size of n units does not match. 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 0 to 1,048,575 bytes.