Ubuntu – How to list all installed packages from a specific category (component)

aptaptitudecommand linedpkgpackage-management

Is it possible to list all installed packages from a specific official repository component (Main, Restricted, Universe or Multiverse) using utilities like apt, apt-cache, aptitude?

I wrote a simple script for this purpose:

dpkg -l | grep ^ii | cut -f3 -d ' ' | while read -r pkg;
do
 status=`apt-cache show $pkg | grep -m1 "Section: multiverse"`
 if [ ! -z "$status" ] 
 then
  echo $pkg
 fi
done;

It works, but it's really slow cause it's checking all packages one by one. Running the time command for this script will produce:

real    1m16.797s
user    0m57.008s
sys     0m8.260s

I already tried aptitude search patterns, and dpkg-query formating, but it seems they don't have the proper column/schema to create a query for this purpose.

I also had a look at vrms script to find out how it's works, because it's really fast in finding contrib/non-free packages, it's seems that vrms script scans the whole /var/lib/dpkg/status file, looking for things like 'Section: (contrib | non-free | restricted | multiverse | partner )', so it wasn't helpful either, because not all packages have this section.

Best Answer

  • Okay, I didn't found any solution to do this with standard utilities, however after having a look at vrms I came-up with a pretty better script to search for packages installed from a specific component.

    The other script which I mentioned in my question was really time consuming.
    However, the new script is available here: pkgs-from.sh

    The usage is:

    ./pkgs-from.sh universe # or main, multiverse, backports
    

    And the time command result for this one is:

    real    0m4.367s
    user    0m0.980s
    sys     0m0.408s
    

    Which is pretty good.

    How it works?
    The script will create a list of all packages related to the requested component from related files within the /var/lib/apt/lists/ directory, then starts to search throughout them instead of using apt-cache.