Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Last active March 21, 2024 03:47
Show Gist options
  • Save sixtyfive/9c0d72b64a5419d773f92a5f5e1219b0 to your computer and use it in GitHub Desktop.
Save sixtyfive/9c0d72b64a5419d773f92a5f5e1219b0 to your computer and use it in GitHub Desktop.
List installed Alpine Linux packages together with their installed sizes in human-friendly format
#!/bin/bash
# there's a tool called numfmt which does the bytes thing for you
# numfmt can also absorb stdin, so the entire thing could just be
# `apk info |
# numfmt --to=iec | # (might need --invalid=ignore and --field=...)
# awk -v OFS="\t" '... {print size, pkg}'
# or you can use awk to do the bytes stuff and use `apk info | awk ...'
bytesToHuman() {
b=${1:-0}; d=''; s=0; S=(Bytes {K,M,G,T,P,E,Z,Y}iB)
while ((b > 1024)); do
d="$(printf ".%02d" $((b % 1024 * 100 / 1024)))"
b=$((b / 1024))
let s++
done
echo "$b$d ${S[$s]}"
}
INSTALLED=$(apk info)
for PKG in $INSTALLED; do
# sed '/[a-z]/ { ...; q}' instead of the grep | grep | tail
SIZE=$(apk info $PKG | grep -vE '[a-z]' | grep -E '[[:digit:]]' | tail -1)
SIZE=$(bytesToHuman $SIZE)
echo -e "$SIZE\t$PKG"
done
@will-ca
Copy link

will-ca commented Mar 21, 2024

Shorter:

apk list -Iq | while read pkg; do apk info -s "$pkg" | tac | tr '\n' ' ' | xargs | sed -e 's/\s//'; done | sort -h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment