linux find: searching for files smaller than 2Kb misses the ones between 1 and 2Kb

linux find: searching for files smaller than 2Kb misses the ones between 1 and 2Kb

I just stumbled in an oddity with the find command on Linux, in a nutshell I had the need to find all files smaller than 2Kb in a certain folder, so I issued the command:

find /folder_to_search_into/ -type f -size -2k

which according to the man page of find should return everything smaller than 2Kilobytes

But what I noticed is that it missed a number of files, which were between 1 and 2Kb in size.

I then issued

find /folder_to_search_into/ -type f -size -2048c

which should be the exact equivalent since "c" stands for bytes, and in this case it found all the files I was looking for.

Any idea as of why this behavior? I'm probably missing the obvious here. Not sure if it's related to this note (from the find man page -- in the size section):

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 behavior of -ls.

答案1

2k is not equivalent to 2048c.

For demonstration I created files named corresponding to their size in bytes:

$ ls -l
total 36
-rw-r--r-- 1 lesmana lesmana 1000 2013-10-03 15:29 s1000
-rw-r--r-- 1 lesmana lesmana 1023 2013-10-03 15:32 s1023
-rw-r--r-- 1 lesmana lesmana 1024 2013-10-03 15:32 s1024
-rw-r--r-- 1 lesmana lesmana 1025 2013-10-03 15:32 s1025
-rw-r--r-- 1 lesmana lesmana 2000 2013-10-03 15:29 s2000
-rw-r--r-- 1 lesmana lesmana 2047 2013-10-03 15:37 s2047
-rw-r--r-- 1 lesmana lesmana 2048 2013-10-03 15:37 s2048
-rw-r--r-- 1 lesmana lesmana 2049 2013-10-03 15:37 s2049
-rw-r--r-- 1 lesmana lesmana 3000 2013-10-03 15:29 s3000

Here is the dd command I used to create the files:

dd if=/dev/zero of=filename bs=1 count=filesizeinbytes

Observe:

$ find -size 2048c
./s2048
$ find -size 2k
./s2000
./s1025
./s2047
./s2048

and:

$ find -size 1024c
./s1024
$ find -size 1k
./s1023
./s1000
./s1024

The logic of -size 2048c means all files with size in bytes is 2048. The logic of -size 2k means all files with size rounded up to the next kilobyte is 2

Observe further:

$ find -size -2k
./s1023
./s1000
./s1024
$ find -size -2048c
./s2000
./s1023
./s1025
./s2047
./s1000
./s1024

The logic of -size -2048c is any file which size in bytes is less than 2048. The logic of -size -2k is any file which size rounded up to the next kilobyte is less than 2.

相关内容