views:

29

answers:

2

I used to have this sweet alias for du I called dusort that would print out a human-readable list of sizes for the top-level subdirectories+files sorted by size. It was like a mini-filelight for mac that runs in terminal.

But now my alias is broken after I copied it to my new mac running Mac OS 10.6. Apparently the sort I used either came from fink (which I am trying to avoid re-installing on my new mac) or the one shipping with 10.6 has less features than the one shipping with 10.4 (far less likely.)

Here is the old alias that's a bit kludgy because it has to run du twice to get both the machine-readable and human-readable file sizes (I actually have this saved as a script in ~/bin with a #!/bin/bash but that shouldn't matter): sort -n +1 <(paste <(du -hd1|cut -f1) <(du -d1))|cut -f1,3

Any ideas on: A. Making it work again? B. Making this command more elegant using bash wizardry?

I know I could fix this by copying the du output to a temp file or fifo or some such nonsense, but this is getting ridiculous. I decided to come here to get assistance in kicking my kludgy bash habits. Please advise. :)

A: 

Now that I think about it, this works:

sort <(paste <(du -d1|cut -f1) <(du -hd1))|cut -f2,3

Still, seems like a giant kludge, especially calling du twice which could make the difference between 5 minutes and 10 minutes on a large file tree... any advice on cleaning it up?

Eliot
Better to edit your question than create an answer: http://stackoverflow.com/faq
danio
A: 

I use this on red hat linux, maybe it will also work on BSD?:

du -sk * | sort -n | awk '
{ if ($1 < 1024) { output("K", 1) }
  else if ($1 < 1048576) { output("M", 1024) }
  else { output("G", 1048576) }
}

function output(size, div)
{
  printf "%d%s\t%s\n", ($1/div), size, $2
}
'

Or to set up as an alias some quoting is required:

alias dusort='du -sk * | sort -n | awk '\''
{ if ($1 < 1024) { output("K", 1) }
  else if ($1 < 1048576) { output("M", 1024) }
  else { output("G", 1048576) }
}
function output(size, div)
{
  printf "%d%s\t%s\n", ($1/div), size, $2
}
'\'''

If you have any terabyte files you would need to extend it...

danio
Interesting... when I started out on this years ago I tried doing it in awk first but gave up.I wonder why your script gives vastly different sizes than mine? See:dusort bash returns7.4G .dusort awk returns 14.8G .This thing won't let me add proper spacing so I took out most of the above list.
Eliot
Probably because block size is 512-bytes on MacOSX. I've added -k to fix that.
danio