Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Reminds me of someone's script I have been using for over a decade.

    #/bin/sh
    du -k --max-depth=1 "$@" | sort -nr | awk '
         BEGIN {
            split("KB,MB,GB,TB", Units, ",");
         }
         {
            u = 1;
            while ($1 >= 1024) {
               $1 = $1 / 1024;
               u += 1
            }
            $1 = sprintf("%.1f %s", $1, Units[u]);
            print $0;
         }
        '


I don't understand the point of the script, it's nothing more than:

  du -h --max-depth=1 "$@" | sort -hr


`-h` is not available in all `sort` implementations


Even the busybox port has it. The only sort implementation I know of that doesn't have -h is toybox (I guess older busybox implementations are missing it as well), but I'm using -h for well over a decade and seldom had it missing


i was actually curious when busybox's sort added it; but didnt search too hard. was certainly easy to see gnu get it in 2009 i think (but even then if the dude setup there bashrc long ago and that func/alias works, likely no reason to change it immediatly)

i can say an `BusyBox v1.35.0 (2022-08-01 15:14:44 UTC)` did not have -h; so it having it now is kind of a shock to me (looks like busybox v1.36.1 has it - at least from 2023-06-22) - good too! always frustrating when a dev tries using gnu-args and it blows up and i gotta explain the diff between mac-shell-cmds, gnu, and busybox


I found this online a long time ago, and it's been with me across BSD, Macintosh and Linux. So I can't say why it is that way, and I didn't know about sort -h before today.


The point is that it is faster.


A bash script for postprocessing the sorting is certainly slower than just having sort do it correctly in the first place.


Any particular reason for doing the human readable units "manually"? `du -h | sort -h` works just fine.


Nice!


I will definitely try this one and compare with my daily stuff

`du -s -k * | sort -r -n -k1,1 -t" "`




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: