Find packages installed from a certain repository with aptitude

39

22

I'm in search for something analog to this question: Zypper: How do I display all packages from a certain repository?

Since I'm on Ubuntu, I need an aptitude based solution: How can I get a list of installed packages from a certain repository?

Boldewyn

Posted 2010-04-18T17:40:28.487

Reputation: 3 835

Answers

17

Using aptitude, in order to look for installed packages outside of the stable branch, you can use:

aptitude search "?narrow(?installed,?not(?archive(stable)))"

To see versions as well as package-names (and instead of descriptions) you can use the command with the format option (-F for short), as follows.

aptitude search -F "%p %V %v" "?narrow(?installed,?not(?archive(stable)))"

For more formats, please take a look at the manpage (here's documentation with avail. options).

That works for example, in Debian if you installed packages outside Squeeze (by runing, for example, apt-get install -t sid package-name.

You can look where an installed package comes from via apt-cache policy, usage is as follows:

apt-cache policy <package-name>

For example, my python-numpy package renders the following output:

$ LANG=C apt-cache policy python-numpy
python-numpy:
  Installed: 1:1.6.2-1
  Candidate: 1:1.6.2-1.2
  Version table:
     1:1.7.0-1 0
          1 http://ftp.es.debian.org/debian/ experimental/main amd64 Packages
     1:1.6.2-1.2 0
        500 http://ftp.es.debian.org/debian/ sid/main amd64 Packages
 *** 1:1.6.2-1 0
        100 /var/lib/dpkg/status
     1:1.4.1-5 0
        990 http://ftp.es.debian.org/debian/ squeeze/main amd64 Packages
        990 http://ftp.de.debian.org/debian/ squeeze/main amd64 Packages

That means that I'm one version behind current sid/main's branch, so I have an old-sid version installed. I see I don't have the stable one because it is yet 1.4.1-5, and I'm currently at 1.6.2-1.

At time of submittal this package was already updated : )

ssice

Posted 2010-04-18T17:40:28.487

Reputation: 756

I prefer ?any-version(), because ~narrow(pat1, pat2) is the same as ?any-version(pat1 pat2) and it supports ?any-version(pat1 pat2 pat3) without looking stupid. – Robert Siemer – 2014-03-15T18:04:37.877

18

After reading info page of aptitude and a dozen of attempts, I finally got this :

aptitude search '?narrow(?installed,?not(?archive(testing)) ?archive(unstable))'

or (equivalent) :

aptitude search '~S ~i (!~Atesting ~Aunstable)'

It will search packages installed from unstable archives of any repository. You have to filter out packages from your default archive (testing in above example).

If you want to filter packages installed from www.debian-multimedia.org/unstable :

aptitude search '~S ~i (!~Atesting ~Aunstable ?origin("Unofficial Multimedia Packages"))

Edit: “Archive”, “origin” etc. are deducted from the Release file of the repository. Unfortunately not all tools can look at all those lines and they use different syntax for them. You can find those files as /var/lib/apt/lists/*Release. Or just type apt-cache policy to get an overview. apt-cache changed its output format: later versions use apt_preferences style.

  • Suite: or Archive: (old name!)
    • aptitude search: ?archive(___) or ~A___
    • aptitude format: %t
    • apt_preferences: release a=___
    • Ubuntu examples: natty-backports, trusty-security, stable
  • Origin:
    • aptitude search: ?origin(___) or ~O___
    • aptitude format: n/a
    • apt_preferences: release o=___
    • Ubuntu examples: Canonical, Google, Inc., LP-PPA-dockbar-main, Ubuntu
  • all other lines
    • aptitude: n/a

Christophe

Posted 2010-04-18T17:40:28.487

Reputation: 181

15

Examine the origin tag (such as o=Debian) for each of your current repositories:

apt-cache policy | sed -n 's/.*o=\([^,]\+\).*/\1/p' | uniq

Then search for packages from (or not from) a particular origin:

aptitude search "?installed?origin(Debian)"
aptitude search "?installed?not(?origin(Debian))"

This is not suitable for a security audit because it relies on each repository to provide its own origin information, but it might be helpful for troubleshooting the origin of packages that are present in multiple repositories.

Anonymous

Posted 2010-04-18T17:40:28.487

Reputation: 151

2Your aptitude search patterns are not good. – They select like this “list packages where one version is installed, and any other version exists in Debian” or “some version installed, and at least one version of that package available on non-Debian”. – What we look for is “the version installed is from Debian (or not)”. This has to be done with ?narrow() or ?any-version(). – Robert Siemer – 2014-01-26T05:09:23.297

Confirmed. I've installed nemo-fileroller from linuxmint, but it exists in Debian, too. So it appears in the list, because it's installed and exists somewhere from Debian. – Boldewyn – 2014-03-11T09:44:58.707

6

For completeness: On Ubuntu systems you can use Synaptic for this task, too. In the left column you can filter packages by their origin.

Boldewyn

Posted 2010-04-18T17:40:28.487

Reputation: 3 835

I was trying to do this and found this SU post via Google, can't believe I overlooked Synaptic, thanks! – David Claridge – 2010-11-27T20:33:26.147

@RobertSiemer It might have slipped your attention, that I accepted it after two days w/o sufficient answer, and before any of the other answers dropped in. I accepted it, because it solved my problem. I also upvoted all later answers, but, I beg your pardon, never changed the state of acceptness. You will also note on this site’s FAQ, that I haven't drawn any reputation gains from this move, where in fact I've lost 2 reps for not accepting someone else's answer. So please take a step back in the future before judging someone's decisions in plain public and rethink the possible causes. – Boldewyn – 2014-01-26T20:47:15.220

2

I've found this :

aptitude search "?origin (<repository>) ?installed"

You can also find a list of search terms supported by "aptitude search" here.

max

Posted 2010-04-18T17:40:28.487

Reputation: 29

Why do you need to run this with sudo ? It seems to me that this also works without running it as root. – Andre Holzner – 2010-12-04T17:15:17.560

It doesn't need to be run with sudo – Kurtis Nusbaum – 2012-08-04T15:13:57.497

As I commented on @Anonymous answer, this does not have the desired effect. – Robert Siemer – 2014-03-11T05:05:51.087

2

First, find the appropriate file(s) for the repository of interest in /var/lib/apt/lists. It should be possible to do this programmatically, but I haven't needed to do so.

Using google chrome as an example, try this:

SEARCH_PATTERN=dl.google.com_linux_chrome  # adjust to suit your needs

for PKG in $( grep-dctrl -sPackage . /var/lib/apt/lists/${SEARCH_PATTERN}_*_Packages | sed 's/^Package: //' )
do
    if dpkg -s "${PKG}" 2> /dev/null 1> /dev/null
    then
        echo ${PKG}
    fi
done

and I get the output:

google-chrome-stable

dpkg -s returns 0 if the package is installed and nonzero otherwise. For future reference purposes, the output of

grep-dctrl -sPackage . /var/lib/apt/lists/${SEARCH_PATTERN}_*_Packages

was

Package: google-chrome-beta
Package: google-chrome-stable
Package: google-chrome-unstable

coreyh

Posted 2010-04-18T17:40:28.487

Reputation: 21

2@RobertSiemer How is it relevant? The question, assuming a reasonable command of English, is how to find the source of packages that were installed with aptitude. There's no requirement that the solution use aptitude. – Auspex – 2015-04-08T03:03:14.043

@Auspex, I believe otherwise. – And don’t worry, my command of English surpassed “reasonable” a couple of years ago. – Robert Siemer – 2015-04-08T07:28:09.490

2Apparently not... – Auspex – 2015-04-08T20:05:04.080

2@Auspex: are you trying to provoke me? The opposite is true: the question is asking for a solution based on aptitude, not for packages installed with aptitude. – Robert Siemer – 2015-04-26T14:46:15.123

@Boldewyn can you clarify what Auspex or I say? – Robert Siemer – 2015-04-26T17:43:57.927

...does this use aptitude? – Robert Siemer – 2014-01-26T05:19:19.660

0

The other answers are actually incorrect, because the parameter to ?archive() is a regular expression. So ?archive(stable) matches both stable and unstable. To exclude just stable you need to anchor the regex pattern:

aptitude search -F "%p %V %v %t" '?any-version(?installed ?not(?archive("^stable$")))'

To exclude multiple repositories:

aptitude search -F "%p %V %v %t" '?any-version(?installed ?not(?archive("^(xenial|xenial-updates)$")))'

Also note that some packages belong to multiple repositories, e.g. xenial-security,xenial-updates. ?archive() evaluates the regex pattern against each repository individually, so ?archive("^xenial-updates$") will match any packages belonging to xenial-updates, even if it belongs to other repositories too.

Animism

Posted 2010-04-18T17:40:28.487

Reputation: 121