Last active
August 29, 2015 14:24
-
-
Save alkhe/735f940ac67b128a26bc to your computer and use it in GitHub Desktop.
Short script to sort du -h output by size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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