Created
November 7, 2022 11:01
-
-
Save smontanaro/ddb77484bb4673623c23c8e71ef7e04a to your computer and use it in GitHub Desktop.
I got this du postprocessing script from "the net" in the dark ages (probably from Usenet in the 80s or 90s). I have no idea who the original author was. I've never modified it **at all**. It has always just worked.
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
#!/bin/sh | |
# | |
# sort a "du" listing by directory size | |
# usage: du | dusort | |
FILES= | |
TFORM=0 | |
while test $# -ge 1; do | |
case $1 in | |
-t) TFORM=1; ;; | |
*) FILES="$FILES $1"; ;; | |
esac | |
shift | |
done | |
#build complex keys so that subdirectories move with parent | |
awk '{ size[ $2] = $1 } | |
END { | |
for (i in size) { | |
printf "%s ", i; | |
oj = 1; l = length(i); | |
#build up an aggregate key from all its parents | |
for (j = 1; j <= l; ) { | |
for (; j <= l; j++) if (substr(i,j,1) == "/") break; | |
name = substr(i, oj, j-oj); | |
j++; | |
printf "%d ", size[name]; | |
} | |
#print itself once more to compare ahead of its children | |
printf "%d\n", size[i]; | |
} | |
}' $FILES | | |
#sort numerically | |
sort -r -n -k 1,1 -k 2,2 -k 3,3 -k 4,4 -k 5,5 -k 6,6 -k 7,7 -k 8,8 | | |
#just print the path and its size. In two popular flavors. | |
awk '{if('$TFORM') printf "%s(%d)\n", $1, $NF; else | |
printf "%8d\t%s\n", $NF, $1}' | | |
#indent directories | |
# This awk could be combined with the previous one | |
# but it really performs a separate function. | |
# Cut it off and put it in a separate file called 'ind' if you like it. | |
# | |
# ind: indent output from du or find | |
# | |
awk ' | |
BEGIN {blank=" "} | |
{ | |
for (s=length; s > 0 && substr($0, s, 1) > " " ; s--) | |
; | |
for (e=length; substr($0, e, 1) != "/" && e > s+1; e--) | |
; | |
print substr($0, 1, s) substr(blank, 1, e-s-1) substr($0, e); | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment