To find files under 1 MB, write the limit in bytes: find /your/directory -type f -size -1048576c. Do not write -size -1M. On GNU find — which is what you have on Linux — those two are not the same command, and find(1) says so in as many words: “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.”
There is no s suffix in any find, so -size -Xs with s for bytes is a hard error:
$ find t -type f -size -500s
find: -size: -500s: illegal trailing character
The suffix for bytes is c, for characters. Likewise, find /var/log -type f -size -1M -exec ls -lh {} + is not a way to list small log files: run on Linux, it lists your zero-byte log files and nothing else.
Table of Contents
Why the megabyte form breaks
GNU find(1) documents the rule under -size: “File uses less than, more than or exactly n units of space, rounding up.” Rounding up is the whole problem. A 500 KB file occupies a fraction of one mebibyte, that fraction rounds up to 1, and -size -1M asks for fewer than one unit. Only a file of zero bytes uses zero units. Everything else is excluded — not just the near-misses.
The same manual page lists the units: “‘b’ for 512-byte blocks (this is the default if no suffix is used)”, “‘c’ for bytes”, “‘k’ for kibibytes (KiB, units of 1024 bytes)”, “‘M’ for mebibytes (MiB, units of 1024 * 1024 = 1048576 bytes)”. Every suffix except c is subject to the rounding, because every suffix except c describes a unit larger than one byte.
macOS and BSD do not behave the same way — measured
This is the part no summary of the man pages will tell you, and it matters if you write scripts on a Mac and run them on a server. Nine files of known size:
$ ls -l t | awk '{print $5, $9}' | sort -n
0 f_0
1 f_1
512 f_512
1023 f_1023
1024 f_1024
512000 f_500k
1048575 f_1048575
1048576 f_1MiB
2097152 f_2MiB
Ask BSD find for everything under a mebibyte, both ways:
$ find t -type f -size -1048576c | sort
t/f_0
t/f_1
t/f_1023
t/f_1024
t/f_1048575
t/f_500k
t/f_512
$ find t -type f -size -1M | sort
t/f_0
t/f_1
t/f_1023
t/f_1024
t/f_1048575
t/f_500k
t/f_512
Identical, and both correct. BSD’s find(1) explains why: it rounds up only for the default 512-byte block unit — “True if the file’s size, rounded up, in 512-byte blocks is n” — and for a scale indicator it says merely that “the file’s size is compared to n scaled as: k kilobytes (1024 bytes), M megabytes (1024 kilobytes)”. Compared, not rounded. So -size -1M is a correct spelling of “under 1 MiB” on macOS and a way to list empty files on Linux. One command, two meanings, no warning either way.
Because the result depends on which find you have, a script rebuilds the nine-file tree, re-runs the size tests on this page and detects whether it is running under GNU or BSD find, so it can assert the right expectation for each and say FAIL, with the claim, if one is wrong on your machine.
The rounding trap is still here — in the default unit
Drop the suffix and BSD rounds too, in 512-byte blocks:
$ find t -type f -size -1 | sort
t/f_0
A bare number is not bytes and is not kilobytes. Every file of 1 byte or more occupies at least one block, so -size -1 asks for fewer than one block and only the empty file qualifies. This is the same arithmetic that eats -size -1M on GNU; on BSD it is confined to the unsuffixed form. Either way, the fix is the same: say bytes.
Write the limit in bytes
find /path -type f -size -102400c # under 100 KB (100 * 1024)
find /path -type f -size -1048576c # under 1 MiB
find /path -type f -size -10485760c # under 10 MiB
-size -Nc means the same thing on both implementations, needs no mental model of rounding, and survives being copied between a laptop and a server. The arithmetic is the only cost.
Before you add -delete
Take find /tmp -type f -size -100k -delete: small files in /tmp are overwhelmingly lock files, PID files and sockets belonging to programs that are running right now; deleting them is how you make a database think it crashed. If you must, preview with -print and re-run the identical expression with -delete:
find /path -type f -size -102400c -print
find /path -type f -size -102400c -delete
Two separate runs on an unchanged tree, not -print -delete in one command. The script linked above does exactly this against its own scratch directory and asserts that the survivors match the preview.
More on -size, finding large files and counting files
- The find command tutorial has the measured comparison of
-size 1,-size 1cand-size 1kagainst files of 1, 512, 513, 1024 and 1025 bytes, plus why-exec … {} +beats-exec … \;and how-regexsilently matches nothing. - Going the other way — finding large files — is a different problem, because
du,lsandfinddisagree about what a file’s size even is. Sparse files are the extreme case: 100 MB tofind -size, 4 KiB todu. - If what you actually want is how many rather than which, see counting files in a directory, where every common one-liner returns a different wrong number.