You are not logged in.

#1 2007-12-30 17:04:02

DocXcz
Member
Registered: 2007-12-30
Posts: 2

Total size of installed packages - script

Hi,

I spend few hours today to make bash script, that allow me get total size of installed packages on my disk. It may be useful for many users.

Here it is:

#!/bin/bash

total=0
progress="-"
count=`pacman -Q | wc -l`

echo "Reading info of "$count" packages reported by 'pacman -Q'.
This may take few minutes..."

for pkg in `pacman -Q | sed 's/\ .*//'`
do
    s=`pacman -Qi $pkg | sed -e '/Installed\ Size/!d; s/Installed\ Size\ :[\ ]*//; s/\ K//; s/,/./'`
    if [ "$s" != "" ];  then
        total=`echo $total" + "$s | bc`
        printf "\033[1D"$progress
        if [ "$progress" == "|" ]; then progress="-"; fi;
        if [ "$progress" == "\\" ]; then progress="|"; fi;
        if [ "$progress" == "_" ]; then progress="\\"; fi;
        if [ "$progress" == "/" ]; then progress="_"; fi;
        if [ "$progress" == "-" ]; then progress="/"; fi;
    fi
done

echo
echo "Total size of installed packages is "$total" K"

! Script requere bc command (pacman -S bc) !

Thank you for corrections and comments.

Offline

#2 2007-12-30 18:48:39

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: Total size of installed packages - script

I added a crude human-readability block near the end.

#!/bin/bash

total=0
progress="-"
count=`pacman -Q | wc -l`

echo "Reading info of "$count" packages reported by 'pacman -Q'.
This may take few minutes..."

for pkg in `pacman -Q | sed 's/\ .*//'`
do
    s=`pacman -Qi $pkg | sed -e '/Installed\ Size/!d; s/Installed\ Size\ :\ *//; s/\ K//; s/,/./'`
    if [ "$s" != "" ];  then
        total=`echo $total" + "$s | bc`
        printf "\033[1D"$progress
        if [ "$progress" == "|" ]; then progress="-"; fi;
        if [ "$progress" == "\\" ]; then progress="|"; fi;
        if [ "$progress" == "_" ]; then progress="\\"; fi;
        if [ "$progress" == "/" ]; then progress="_"; fi;
        if [ "$progress" == "-" ]; then progress="/"; fi;
    fi
done

suffix='K'
if [ `echo "$total > 1024" | bc` = 1 ]; then
        total=`echo "scale=2; $total / 1024" | bc`
        suffix='M'
        if [ `echo "$total > 1024" | bc` = 1 ]; then
                total=`echo "scale=2; $total / 1024" | bc`
                suffix='G'
        fi
fi

echo
echo "Total size of installed packages is $total $suffix"

And here's a version with a simpler and more informative progress meter:

#!/bin/bash

total=0
progress="1"
count=`pacman -Q | wc -l`

echo "Reading info of "$count" packages reported by 'pacman -Q'.
This may take few minutes..."

for pkg in `pacman -Q | sed 's/\ .*//'`
do
        printf "\rprocessing file "$(( progress++ ))"..."
        s=`pacman -Qi $pkg | sed -e '/Installed\ Size/!d; s/Installed\ Size\ :\ *//; s/\ K//; s/,/./'`
        if [ "$s" != "" ];  then
                total=`echo $total" + "$s | bc`
        fi
done

suffix='K'
if [ `echo "$total > 1024" | bc` = 1 ]; then
        total=`echo "scale=2; $total / 1024" | bc`
        suffix='M'
        if [ `echo "$total > 1024" | bc` = 1 ]; then
                total=`echo "scale=2; $total / 1024" | bc`
                suffix='G'
        fi
fi

echo
echo "Total size of installed packages is $total $suffix"

It works, good job. I didn't know about bc!

Oh, and welcome to the Arch BBS!

Last edited by peets (2007-12-30 18:49:22)

Offline

#3 2007-12-30 20:18:10

DocXcz
Member
Registered: 2007-12-30
Posts: 2

Re: Total size of installed packages - script

Yeah smile this is good tune.
I'm really happy of that there is somebody who takes interest in it.

But I don't know how to speed up the script. Is slowness pacman or script cause? Maybe use of tupac can speed up script. On the other hand perhaps nobody run script quickly times over wink

And I'm sorry for my english, if it is horrible tongue

Offline

#4 2017-12-26 15:47:43

livix
Member
Registered: 2012-08-01
Posts: 6

Re: Total size of installed packages - script

Here is an updated version (for pacman v5.0.2):

#!/bin/bash

total=0
progress="1"
count=$(pacman -Q | wc -l)

echo "Reading info of "$count" packages reported by 'pacman -Q'.
This may take few minutes..."

for pkg in $(pacman -Q | sed 's/\ .*//')
do
        printf "\rprocessing package "$(( progress++ ))" $pkg..."
        s=$(pacman -Qi $pkg | sed -e '/Installed\ Size/!d; s/Installed\ Size\ *:\ *//')
        if [ "$s" != "" ];  then
		s1=$( echo $s | sed -e 's/\ B//; s/\ KiB//; s/\ MiB//; s/\ GiB//' )
		s2=$( echo $s | sed -e "s/$s1//" )
		s1=$( echo $s1 | sed -e 's/,/./' )
		echo $s | sed -e '/\ B/q0; /\ KiB/q1; /\ MiB/q2; /\ GiB/q3'
		factor=$( echo $? )
                total=$(echo "$total + $s1 * 2 ^ ( 10 * $factor )" | bc)
        fi
done

suffix='B'
if [ $( echo "$total > 1024" | bc ) = 1 ]; then
        total=$( echo "scale=2; $total / 1024" | bc )
        suffix='KiB'
        if [ $( echo "$total > 1024" | bc ) = 1 ]; then
                total=$( echo "scale=2; $total / 1024" | bc )
                suffix='MiB'
		if [ $( echo "$total > 1024" | bc ) = 1 ]; then
		        total=$( echo "scale=2; $total / 1024" | bc )
		        suffix='GiB'
		fi
	fi
fi

echo
echo "Total size of installed packages is $total $suffix"

Offline

#5 2017-12-26 19:02:24

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Total size of installed packages - script

Meh.

expac -H M '%m' | awk '{sum += $1}END {print sum, $2}'

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#6 2017-12-26 21:38:04

N_BaH
Member
Registered: 2015-12-30
Posts: 84

Re: Total size of installed packages - script

+1 expac
--
why not just loop through pacman -Qi | grep 'Installed Size' using a while read ?

while read -ra instSize
do
   size=${instSize[3]}
   unit=${instSize[4]}
   case $unit in
   GiB) factor=3;;
   MiB) factor=2;;
   KiB) factor=1;;
   esac
   Total=$(echo "${Total:-0} + $size * 2^(10*${factor:-0})" | bc)
done < <(LC_ALL=C pacman -Qi | grep 'Installed Size')

Offline

#7 2017-12-26 22:01:32

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: Total size of installed packages - script

If not expac, no need to call bc several hundred times.  Assemble the arithmetic, then pass it to bc once:

pacman -Qi | sed -n '/^Installed/{s/.*: //;H} ${x;s/^/0 /;s/GiB/* 1073741824/g;s/MiB/* 1048576/g;s/KiB/* 1024/g;s/B//g;s/\n/ + /gp}' | bc

One call to pacman, one call to sed, and one call to bc, that's it.

This gives the size in bytes, but if you prefer KiB or MiB you can modify the unit replacement commands in sed script, for example, this is for KiB:

pacman -Qi | sed -n '/^Installed/{s/.*: //;H} ${x;s/^/0 /;s/GiB/* 1048576/g;s/MiB/* 1024/g;s/KiB//g;s/B/\/ 1024/g;s/\n/ + /gp}' | bc

Last edited by Trilby (2017-12-26 23:02:52)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

Board footer

Powered by FluxBB