It turns out that there is no standard “apt” command which lists where a package was installed from. You may need this information if you have added additional APT repositories to your Debian/Ubuntu installation. I see a lot of questions at the forums (1, 2, 3, 4) and the proper solution tends to be “parse apt-cache output yourself”. Here is my solution which is very similar to this one:
#!/bin/bash set -u errors=0 for PKGNAME in $(dpkg -l|grep ^i|awk '{print $2}'); do INFO="$(apt-cache policy "$PKGNAME")" IVER="$(echo "$INFO" | grep Installed: | awk '{print $2}')" IPRIO="$(echo "$INFO" | fgrep "*** $IVER" | awk '{print $3}')" REPO="$(echo "$INFO" | fgrep -A1 "*** $IVER" | tail -n+2 | head -n1 | awk '{print $2 " " $3}')" echo "$PKGNAME repo=$REPO" if [ "$REPO" == '' ]; then errors=$(( $errors + 1 )) echo "ERROR: Unable to find the repo for package \"$PKGNAME\"" >&2 fi done if [ "$errors" -ne 0 ]; then echo "ERROR: $errors errors encountered" >&2 exit 1 else exit 0 fi
May 25, 2022 at 6:31 pm
Mr Zahariev,
Would you be able to licence this script under CC0 or at least CC BY-SA so that more people can know about it on AskUbuntu and other places?
May 26, 2022 at 9:08 pm
Hi. Feel free to use the script under the MIT license.