3

I've found a bazillion references to get a list of the installed packages, but how can I print a single list of all known packages with their "package state" (not-installed, installed, half-installed etc. as defined by dpkg) in a shell, ideally like this:

awk   not-installed
bash  installed
cc    half-installed
[...]

dpkg --get-selections and dpkg --list only lists installed packages.

dpkg --get-selections '.' does not work.

apt-cache dump does not print whether packages are installed, and also prints a lot of irrelevant stuff.

I'm using Travis CI, which is running Ubuntu 12.04 LTS Server Edition 64 bit with for example dpkg-query 1.16.1.2.

l0b0
  • 8,529
  • 8
  • 42
  • 67
  • And what does `dpkg --get-selections` output? Why do you run it with `.`? – Pilot6 Oct 03 '15 at 17:09
  • @Pilot6 Because the docs said it supports patterns, but apparently only patterns which start with an alphanumeric character (which sort of defeats the purpose if you're trying to list everything). – l0b0 Oct 03 '15 at 17:15
  • Possible duplicate of [How to get list of installable packages from repositories?](http://askubuntu.com/questions/220478/how-to-get-list-of-installable-packages-from-repositories) – Pilot6 Oct 03 '15 at 17:35
  • @Pilot6 [No, that doesn't seem to print whether the packages are installed or not.](https://travis-ci.org/l0b0/travis-ci-helper/builds/83472459) – l0b0 Oct 03 '15 at 17:40
  • So `dpkg -l` shows the installed packages and `apt-cache dump` shows all of them. – Pilot6 Oct 03 '15 at 17:41
  • `apt-cache dump` also shows a *lot* of other package information. Not ideal, but I'm just about ready to give up on this and just brute force it with a `while read` loop. – l0b0 Oct 03 '15 at 17:48

2 Answers2

1

You want dpkg-query;

For dpkg-query >= 1.17.11:

dpkg-query -f '${Package}\t${db:Status-Status}\n' -W '*'

For dpkg-query < 1.17.11:

dpkg-query -f '${Package} ${Status}\n' -W '*' | awk '{print $1"\t"$4}'

#1:

  • -f '${Package}\t${db:Status-Status}\n': When used with the -W option, specifies the format of the output (see man dpkg-query for other options);
  • -W '*': lists all the packages matching the pattern *;

#2:

  • -f '${Package} ${Status}\n': When used with the -W option, specifies the format of the output (see man dpkg-query for other options);
  • -W '*': lists all the packages matching the pattern *;
  • awk '{print $1"\t"$4}': prints only the first and fourth field;

In this case it seems like you want to list the status word, so I picked the db:Status-Status virtual field; here are the other virtual fields related to the package status:

              db:Status-Abbrev
                     It contains the abbreviated package status, such as  "ii"
                     (since dpkg 1.16.2).

              db:Status-Want
                     It contains the package wanted status, part of the Status
                     field (since dpkg 1.17.11).

              db:Status-Status
                     It contains the package status word, part of  the  Status
                     field (since dpkg 1.17.11).

              db:Status-Eflag
                     It  contains  the  package status error flag, part of the
                     Status field (since dpkg 1.17.11).

user@user-X550CL ~/tmp % dpkg-query -f '${Package}\t${db:status-status}\n' -W '*' | head
aalib1  not-installed
account-plugin-aim  installed
account-plugin-empathy  not-installed
account-plugin-facebook installed
account-plugin-flickr   installed
account-plugin-foursquare   not-installed
account-plugin-gadugadu not-installed
account-plugin-generic-oauth    not-installed
account-plugin-google   installed
account-plugin-groupwise    not-installed
kos
  • 35,535
  • 13
  • 101
  • 151
0
dpkg-query -l '*'

will show you all the installed, uninstalled and half installed packages. Just grep on the package you want to filter.

infoclogged
  • 531
  • 4
  • 16