Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
1  
Off the top of my head, you should check what the file-sizes actually are, it is dependent on the file-system (EXT4 minimum is 4k). Also calculations based on 1000 vs 1024. –  justbrowsing Oct 3 '13 at 7:38
    
The file system is EXT4, and the actual size of a file that doesn't get found using the -size -2k parameter is for example 1343 bytes. –  adriano72 Oct 3 '13 at 8:24

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Wow thank you so much for going all this way to explain the behavior I reported. This is very helpful -- and makes perfect sense. Definitely something I'll keep in mind. –  adriano72 Oct 3 '13 at 17:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.