Skip to content

Instantly share code, notes, and snippets.

@alkhe
Last active August 29, 2015 14:24
Show Gist options
  • Save alkhe/735f940ac67b128a26bc to your computer and use it in GitHub Desktop.
Save alkhe/735f940ac67b128a26bc to your computer and use it in GitHub Desktop.
Short script to sort du -h output by size.
let sizemap = {
'': 0,
K: 1,
M: 2,
G: 3
};
let data = '`du -h --maxdepth 1` output';
let files = data
.split(/\n/)
.map(line => line.split(/\s+/))
.sort(([a], [b]) => {
let match = /(\d+(?:\.\d+)?)([KMG]?)/;
let [, an, au] = a.match(match);
let [, bn, bu] = b.match(match);
let ucmp = sizemap[au] - sizemap[bu];
return ucmp || an - bn;
});
files.map(([a, b]) => `${a} ${b}`).forEach(x => {
console.log(x);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment