-1

I sometimes wish human-readable du -h option to be more fine-grained while still human-readable.

Instead of showing:

 14G

it would show something like:

14G 236M 788k 110b

Is there an easy / straightforward / standard way to get this?

  • 1
    Just get the source code and modify to your needs. – ridgy Dec 27 '17 at 16:08
  • 2
    This can help you: unix.stackexchange.com/questions/44040/… – Carpette Dec 27 '17 at 16:20
  • 1
    I think so. Depending on your OS/distribution (you did not tell about) get the full sources of coreutils, then modify ./lib/human.c (and probably ./lib/human.h) as this is the function library used, and then do a configureand make. All those coreutils now will have that human readable format you defined, but as long as you do no make install that doesn't harm. – ridgy Dec 27 '17 at 20:02
  • 1
    Looked a bit deeper in the sources. You'ld probably have to modify human.h for the new buffer length LONGEST_HUMAN_READABLE, and then either modify after target do_grouping or the full function human_readable. – ridgy Dec 27 '17 at 20:18
  • 1
    If I were doing this, I would write a utility (say in awk, perl, or python) that converted a control statement parameter, and if no parameter was present, read from STDIN. The reason is that there are probably a number of situations where you might want the expanded output. So not just "du -s | new_utility", but also "ANY_command | new_utility". And the pipeline could be easily put into a script or a function. Many birds with one stone generality ... cheers, – drl Dec 28 '17 at 2:29
2

Well, there seems to be no easy / straightforward / standard way to do this yet.

Alternative options are (credits to ridgy's, Carpette's, Weijun Zhou's and drl's comments :):

  • write a dedicated small converter utility in bash/awk/python/etc. so that:

    $ echo "789456" | utility  
    770K 976o
    

    Then pipe it to convert du output. You can inspire from this related question. If it runs well, it could also parse the output of any command piped to it like:

    $ du -s | utility
    $ ls -la | utility
    

    You can even alias it forever on you machine to:

    duH() du -s $@ | utility
    
    • pros: Easy to write in any language you prefer.
    • cons: difficult to adapt to any command options (du -s, du -hc, du --si). Any command output (ls -lah, rsync) has to be parsed for finding digits strings meant to represent bytes and transformed without breaking the layout.
  • get into coreutils source code and add a new relevant option suiting your needs. You'll probably have to have a glance at ./lib/human.c. Then once modified, it'll be a matter of ./configure, make, make install so the du on your machine will now have this option implemented.

    • pros: quite straightforward, fast and integrated. May be offered as a future patch to standard du ?
    • cons: you'll have to get into existing C code and understand it first not to break it. You'll have to reinstall your own version of coreutils on any machine you need to use it.

For now, I'm not getting into this soon. Anyway, feel free to post here your own pieces of solutions or alternatives workarounds as they come :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.