GNU sort's -h option

December 31, 2012

I only recently became aware of GNU sort's -h option, which strikes me as a beautiful encapsulation of everything (both good and bad) that people attribute to GNU programs and their profusion of options.

GNU sort's -h is like -n (sort numerically) except that it sorts numerically for GNU's 'humane' numbers, as produced by (for example) GNU du's -h option. This leads naturally to a variant of a little script that I've already talked about:

du -h | sort -hr | less

On the one hand, -h is clearly useful in both commands. Humane numbers are a lot easier to read and grasp than plain numbers, and now GNU sort will order them correctly for you. On the other hand you can see the need for a -h argument to sort as evidence of an intrinsic problem with du -h; in this view, GNU is piling hack on top of hack. The arguable Unix way might be a general hum command that humanized all numbers (or specific columns of numbers if you wanted); that would make the example into 'du | sort -nr | hum | less', which creates a general tool at the price of making people add an extra command to their pipelines.

I don't have any particular view on whether GNU sort's -h option is Unixly wrong or not. I do think that it's (seductively) convenient, and now that I've become aware of it it's probably going to work its way into various things I do.

(This could spark a great debate on what the true Unix way is, but I'm not going to touch that one right now.)


Comments on this page:

From 217.255.121.103 at 2012-12-31 06:21:04:

Good find! I've suffered also from not knowing about this and usually worked around it by piping into awk and doing the calculations there, but this will be a time-saver.

From 68.195.89.131 at 2012-12-31 12:05:20:

ditto! now i can retire a few bash shell functions :-).

From 24.13.36.253 at 2012-12-31 18:49:26:

Huzzah! coreutils (or whatever this is a part of) definitely needs more humane PR!

From 98.150.145.94 at 2013-01-01 02:29:43:

I stumbled across this one about 4 months ago and just couldn't believe it actually existed. Really makes du | sort so much nicer :D

I keep meaning to make a concerted effort to read through the man page of the utilities I use all the time, maybe draw a line in the sand and enforce that from then on the first time I use a command I look at the man page.

From 109.77.87.249 at 2013-01-07 06:42:49:

We're aware of the slightly non "Unixy" nature of sort -h.

There is also a more general numfmt command in the works as per your suggestion:
http://lists.gnu.org/archive/html/coreutils/2012-02/msg00085.html

You can subscribe to a quarterly coreutils summary here:
http://www.pixelbeat.org/patches/coreutils/rss2.xml
There is a new one due any day now.

cheers, Pádraig.

Written on 31 December 2012.
« What I'd have liked to hear about Python 3 from the developers
How our fileserver infrastructure is a commodity setup (and inexpensive) »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Dec 31 03:12:33 2012
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.