Skip to content

Instantly share code, notes, and snippets.

@rsms
Created September 1, 2022 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rsms/87570aa1a839ce4884e7d83a3c3dac84 to your computer and use it in GitHub Desktop.
Save rsms/87570aa1a839ce4884e7d83a3c3dac84 to your computer and use it in GitHub Desktop.
apk: list installed packages sorted by file size (e.g. alpine linux)
#!/bin/ash
# list installed packages sorted by file size
apk info -e -s \* >/tmp/apksize
awk 'NR % 3 == 1' /tmp/apksize | cut -d ' ' -f 1 > /tmp/apkname
awk 'NR % 3 == 2' /tmp/apksize > /tmp/apksize2
while read -r n unit; do
B=$n
case "$unit" in
KiB) B=$(( n * 1024 )) ;;
MiB) B=$(( n * 1024 * 1024 )) ;;
GiB) B=$(( n * 1024 * 1024 * 1024 )) ;;
esac
printf "%12u %4s %-3s\n" $B $n $unit
done < /tmp/apksize2 > /tmp/apksize
paste -d' ' /tmp/apksize /tmp/apkname | sort -n -u | cut -c14-
rm /tmp/apksize /tmp/apksize2 /tmp/apkname
@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

@rsms
Copy link
Author

rsms commented Mar 23, 2024

@will-ca what is ‘tac’?

@will-ca
Copy link

will-ca commented Mar 25, 2024

@rsms cat in reverse. Does what it says on the tin, line-by-line. Puts the size from apk info -s before the name, which I find easier to read and sort. Part of coreutils.

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