brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -size -1M only finds empty files, because find rounds sizes up

Find
~/tips/tip-find-size-minus-1m-rounds

find . -type f -size -1024k

Explains why small files go missing from a find -size search, and how to fix it.

This one looks like a bug the first time. find . -size -1M should list everything under one megabyte, but it only shows empty files.

The reason is rounding. find rounds every file size up to whole units of the suffix you used, before it compares. A 500 byte file counts as 1 megabyte, and 1 is not less than 1. Only a file of 0 bytes rounds to 0 and matches. A 500 byte file and a 2 MB file are both missing from -size -1M; only empty files show up.

The fix is to use a smaller unit, so the rounding doesn’t matter:

find . -type f -size -1024k

That lists every file up to 1 megabyte, the 500 byte one included. For an exact limit, count in bytes with the c suffix: -size -1048576c.

The same rounding bites with +, only less often. -size +1M means “more than 1 after rounding up”, so a file of 1.2 MB counts as 2 and does match. And without a suffix, find counts in 512 byte blocks, which surprises everyone once. I’d always write the suffix.

Further reading: find(1) manual page, GNU findutils manual

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link