OpenWrt Opkg List All Installed Packages Sorted by Size

:!: This script is inspired by opkg-list-user-installed-packages.

:!: This script will list all packages instead of listing only user-installed packages.

Three conditions are applied in determining a package size (in order, if the first one is not applicable then the next one is used):

:!: This script should work on base installation without requiring any additional packages to be installed.

opkg-list-installed-packages-sorted-by-size.sh
#!/bin/sh
 
packagelist=$(ls /usr/lib/opkg/info/*.list)
 
for packagelistpath in $packagelist
do
        packagefiles=$(cat $packagelistpath)
        packagename=$(echo $packagelistpath | sed 's/\/usr\/lib\/opkg\/info\///g' | sed 's/\.list//g')
        if [[ -z "$packagefiles" ]]
        then
                packagesizeb=$(opkg info $packagename | grep Size\: | sed 's/Size\:\ //g')
                if [[ -z $packagesizeb ]]
                then
                        echo -e "-.- KB \t\t$packagename"
                else
                        packagesizekb=$(opkg info $packagename | grep Size\: | sed 's/Size\:\ //g' | awk 'END {printf "%.02f\n", $1/1024}')
                        printf '%-16s' "$packagesizekb KB"; printf '%s\n' "$packagename"
                fi
        else
                packagesizekb=$(for file in $packagefiles; do if [[ -f $file ]]; then du -k $file | cut -f1; fi; done | awk '{s+=$1} END {printf "%.02f\n", s}')
                printf '%-16s' "$packagesizekb KB"; printf '%s\n' "$packagename"
        fi
done | sort -n