Skip to content

Instantly share code, notes, and snippets.

@zvyn
Last active April 17, 2018 20:09
Show Gist options
  • Save zvyn/5694729 to your computer and use it in GitHub Desktop.
Save zvyn/5694729 to your computer and use it in GitHub Desktop.
My disk went full so I wrote the following script to generate a list of all installed packages with their (theoretical) disk-usage as estimated by pacman. #ArchLinux #pacman
#!/bin/bash
function list-package-sizes () {
if [ $1 ]; then
case $1 in
-h|--help)
local errno=0
;;
*)
local errno=1
echo Error: I do not accept any parameters else then \'-h\' or \'--help\'.
;;
esac
echo Returns a sortet list of package-sizes in KiB \(and names\) for all packages installed via pacman.
return $errno
else
pacman -Qq |\
pacman -Qi |\
egrep "(Name +:|Installed Size :)" |\
tr -d "\n" |\
sed -E "s/(Installed Size :|Name +| KiB)//g" |\
sed -E "s/ +/ /g" |\
tr ":" "\n" |\
awk '{print $2,$1}' |\
sort -n
return $?
fi
}
if [[ $0 == *list-package-sizes.bash ]]; then
list-package-sizes "$@"
fi
@jamesan
Copy link

jamesan commented Apr 17, 2018

I noticed a couple of bugs with the text processing.

  • There's now possibly more than one space between "Installed Size" and its corresponding colon.
  • The KiB unit phrase needn't be especially filtered out.
  • Include other size units and sort using them.
diff --git a/list-package-sizes.bash b/list-package-sizes.bash
old mode 100644
new mode 100755
index 3573d84..4d8a51a
--- a/list-package-sizes.bash
+++ b/list-package-sizes.bash
@@ -16,13 +16,14 @@ if [ $1 ]; then
 else
     pacman -Qq |\
         pacman -Qi |\
-        egrep "(Name +:|Installed Size :)" |\
+        egrep "(Name +:|Installed Size +:)" |\
         tr -d "\n" |\
-        sed -E "s/(Installed Size :|Name +| KiB)//g" |\
+        sed -E "s/(Installed Size +:|Name +)//g" |\
         sed -E "s/ +/ /g" |\
         tr ":" "\n" |\
-        awk '{print $2,$1}' |\
-        sort -n
+        awk '{print $2,$3,$1}' |\
+        sed -E 's/ ([KMGT])iB/\1/;s/ B / /' |\
+        sort -h
     return $?
 fi
 }

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