Saturday, December 1, 2012

Shell: Summing up lots of (large) numbers

Sometimes you want to know the exact amount of bytes all the files in a directory tree takes. For example, checking the sum of sizes of all files is a quick way to see if a copy operation went OK - if they are the same, there are reasons to believe that it is OK.

du gives varying numbers from filesystem to filesystem

However, 'du' doesn't always give the right number - is it the size of the directory nodes causing differences?
du -sb .

awk-solutions have major precision problems

The solutions flying across the intertubes using awk, goes awry with large numbers. The following have a ceiling on 2147483647, as that is the max of a 32 bit integer number. Absurdly, awk just displays that if it reaches the limit.
find . -type f -printf "%s \n" | awk '{sum += $1 } END {printf "%i bytes\n",sum }'

You can get around that by going to floats (awk really uses double-precision floating-point for all numbers), but then you loose the whole point, exactness:
find . -type f -printf "%s \n" | awk '{sum += $1 } END { printf "%.1f bytes\n",sum }'

The Solution: bc - arbitrary precision calculator language!

Finding the sum of all the files in a directory tree:
echo `find . -type f -printf "%s+"`0 | bc

Same in GiB (GigaBytes as in 1024^3 bytes), using absurd scale to get exactness ('scale' is bc's concept of decimal precision) :
echo scale=50\; \(`find . -type f -printf "%s+"`'0)/(1024^3)' | bc

If you want MiB or KiB, then change the ^3 to ^2 or ^1 respectively.

Thursday, October 4, 2012

KD-101 smoke detector batteries

I have a six-unit setup of KD-101 Smoke Detectors - they are conveniently linked together via radio.These are OEM units, and thus come packaged in utterly different packages, one, two or three in a package, and can be bought at very different price points (I've found them for 3 for 200 NOK, to 1 for 300!!), so shop around.

Each unit uses both a 9 volt battery and three AA (LR03) 1,5 volt batteries. I wondered what the different batteries were used for; Apparently the unit functions just fine with only the 9v battery - it both runs the Test OK, and it triggers the other alarms just fine. However, it apparently cannot receive a trigger from the other alarms just on the 9v battery - so apparently, the 3 x 1,5v batteries are used to run the receiver radio.

In my (unsuccessful) search of this answer found some instruction manuals, a good one is here (PDF, Norwegian), and another one here (PDF). None told what the different batteries were for, so that's the reason for this post.

When the alarm beeps once every 45 seconds, the 3x AA batteries is to be changed.
When it beeps once every 60 seconds, the 9v battery must be changed. (This difference in beep-timing is according to the Norwegian manual)

I just changed all batteries to use Lithium cells. These typically last 10 years in a smoke detector, and the detector states that it should be exchanged after about 8 years, so I guess we're talking "life-time batteries" then! It cost just short of a 1000 kroners to buy 6 9v and 4x4 1,5 AA batteries - I've never been in the vicinity of paying that much for batteries before - not even car and boat batteries are than expensive! And it's more than I paid for the detectors themselves..!