Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Apt show repositories for currently installed packages

Apt makes some things very difficult. If I want to see what repository a package comes from, I need this crazy oneliner.

apt-cache policy $( dpkg -l | awk '$1 ~ /^ii/ {print $2}' ) | awk '/^[^ ]+:$/{gsub(":$","",$0);a=$0;b="";c=""} /\*\*\*/ {b=$2} $1 > "100" {c=$2"/"$3} $1 == "100" {print a,b,c}'


xfonts-encodings 1:1.0.4-2 http://deb.devuan.org/merged/beowulf/main
xfonts-utils 1:7.7+6 http://deb.devuan.org/merged/beowulf/main
xml-core 0.18+nmu1 http://deb.devuan.org/merged/beowulf/main
xmlto 0.0.28-2.1 http://deb.devuan.org/merged/beowulf/main
xorg-sgml-doctools 1:1.11-1 http://deb.devuan.org/merged/beowulf/main
yad 0.40.0-1 http://deb.devuan.org/merged/beowulf/main
yasm 1.3.0-2+b1 http://deb.devuan.org/merged/beowulf/main
zip 3.0-11+b1 http://deb.devuan.org/merged/beowulf/main

This would have been a little easier with yum/dnf, but that's not the point of today's article.

Bonus

And a quick count of packages for each repository:

apt-cache policy $( dpkg -l | awk '$1 ~ /^ii/ {print $2}' ) | awk '/^[^ ]+:$/{gsub(":$","",$0);a=$0;b="";c=""} /\*\*\*/ {b=$2} $1 > "100" {c=$2"/"$3} $1 == "100" {print a,b,c}' | \
{
   if test -n "${SUMMARIZE}" ;
   then
      awk '$NF ~ /:\/\// {x[$NF]++} END {for(i in x) print i": "x[i]}'
   else
      cat
   fi
}



$ SUMMARIZE=1 ~/bin/apt-summary.sh 
http://deb.devuan.org/merged/beowulf/main: 377
http://deb.devuan.org/merged/ceres/main: 1979
http://deb.devuan.org/merged/ceres/non-free: 2

Ignore the different releases mixed in there together. Let's pretend I know what I'm doing!

Comments