You are not logged in.
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
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
Yeah 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
And I'm sorry for my english, if it is horrible
Offline
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
Meh.
expac -H M '%m' | awk '{sum += $1}END {print sum, $2}'
Offline
+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
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
Offline