Monday, October 29, 2007

Sorting "Human Readable" Files By Size Numerically

Hey There,

This one used to drive me nuts! Not less than few revisions ago, Solaris' implementation of df and du only had the "-k" option, so you could look at file sizes, etc, in terms of kilobytes rather than blocks (which could be variably sized).

Then everything started getting fancy with Solaris 9+ (Linux, I think, sported this option first). They began including the "-h" (Human Readable) option so that you could see file sizes, etc, in very easy to read format (e.g. 1Mb rather than 1024 in kilobytes).

The main problem I had with this was that you could no longer run the command stream "du -sk *|sort -n" and have the files list out in numerical order from smallest to largest (or vice versa). If you ran "du -h|sort -n" you'd almost get the same thing, but 2Mb might be listed right after 1Gb and before 3Gb. The numbers were still in order, but the sizes really weren't being listed out from smallest to largest correctly.

Here's a quick little scriptlet that I put together to avoid this headache. If they haven't fixed it yet, hopefully they will soon (assuming it's an "error," which, technically, it isn't).

Just put the following in a script and run it as "./myscript" (or add command line options, like you would to du, if you want to be more selective):


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/ksh

#
# 2007 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

if [ ! -z $1 ]
then
ARGS="$@"
else
ARGS="*"
fi

/usr/bin/du -sk $ARGS|/usr/bin/sort -n|/usr/bin/awk '{print $2}'|/usr/bin/xargs -ia /usr/bin/du -sh "a"


Voila! The sizes are listed in Human Readable format and they're also correctly listed in size from smallest to largest :)

, Mike