GNU (New?) du Binary Option In macOS 12 Monterey

brockwalters
Contributor II

I was using du to create an extension attribute & noticed it now has a helpful option which obviates some ugly math:

--si “Human-readable” output. Use unit suffixes: Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte based on powers of 1000.

This is nice because the macOS GUI shows file / folder sizes (& has for a while...) using 1000-byte blocks. My Mac on Big Sur does not show the --si option so I am assuming this is a Monterey thing... Not sure?

E.g., you may have run du in a script in the past & got a result like this:

 

% du -h -s ~
 29G	/Users/Brock.Walters

 

So your boss says "Great, how come 'Get Info' says 32GB huh Mr. Smarty Script Guy?"

Screen Shot 2021-11-20 at 10.45.20 AM.png

Well, 1) rounding & 2) 1024-byte blocks vs. 1000-byte blocks.

The solution was maybe something like this:

 

% echo "scale=1; $(du -s ~ | awk '{print $1}')/2000000" | bc | awk '{print int($0+0.5)"G"}' 
31G

 

Which, let's face it, is gross. The --si option makes all of that unnecessary because it calculates in 1000-byte blocks!

 

% du --si -s ~
 32G    /Users/Brock.Walters

 

0 REPLIES 0