Last active
March 21, 2024 03:47
-
-
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
This file contains hidden or 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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shorter: