Sortable list of all packages (dpkg)

I want to dump all installed packages on a system which uses dpkg.

Up to now I use dpkg -l.

But it has one draw back: Sorting the result does not make sense.

Head:

root@aptguettler:~# LANG=C dpkg-query -l| sort | head
+++-===========================================================-=================================================-============-================================================================================
Desired=Unknown/Install/Remove/Purge/Hold
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
ii  a11y-profile-manager-indicator                              0.1.10-0ubuntu3                                   amd64        Accessibility Profile Manager - Unity desktop indicator

Tail:

root@aptguettler:~# LANG=C dpkg-query -l| sort | tail
rc  texlive-publishers-doc                                      2015.20160320-1                                   all          TeX Live: Documentation files for texlive-publishers
rc  texlive-science                                             2015.20160320-1                                   all          TeX Live: Natural and computer sciences
rc  texlive-science-doc                                         2015.20160320-1                                   all          TeX Live: Documentation files for texlive-science
rc  tpconfig                                                    3.1.3-15                                          amd64        touchpad device configuration utility
rc  ttf-indic-fonts-core                                        1:0.5.14ubuntu1                                   all          Core collection of free fonts for languages of India
rc  ttf-punjabi-fonts                                           1:0.5.14ubuntu1                                   all          Free TrueType fonts for the Punjabi language
rc  unity-lens-friends                                          0.1.3+14.04.20140317-0ubuntu1                     amd64        Friends scope for unity
rc  webaccounts-extension-common                                0.5-0ubuntu2.14.04.1                              amd64        Ubuntu Online Accounts browser extension - common files
rc  xfonts-mathml                                               6ubuntu1                                          all          Type1 Symbol font for MathML
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend

I keep a history of this output via etckeeper (Related question with answer log hwinfo output with etckeeper).

Here the things I would like to improve:

  • The ascii-art lines are not nice. The should be removed.
  • The first two characters (for example ii) should be removed or appear at the end.

On rpm based systems rpm -qa does exactly what I need.

Answer

Try

dpkg --get-selections | grep -v deinstall

If you need the exact version of the packets in the output you could do:

dpkg -l | grep '^ii' | awk '{print $2 "\t" $3}'

This only prints the columns 2 and 3. This also only lists installed packages, no uninstalled or others.

Edit: Another option is dpkg-query:

dpkg-query --show --showformat='${Package} ${Version}  ${Architecture} ${db:Status-Abbrev} \n'

Where –showformat (or -f) defines what columns you want to display, in this case the package name, version and architecture and the short status (e.g. “ii” and “rc”) at the end, the “\n” is the linebreak.

By the way, the “ii” defines installed packages, “rc” are uninstalled packages, that’s why I used grep and awk to filter the uninstalled packages out.

If you’re feeling fancy you can add column widths aswell like this:

dpkg-query --show --showformat='${Package;-50} ${Version;-40}  ${Architecture;-5} ${db:Status-Abbrev} \n'

A negative column width means the orientation is left, positive means right.

Be careful though as the package name will be cut short if the width is smaller than the number of characters in a package name.

I’m not really sure for which purpose you need the list. If you just want to have a list which is well readable there is nothing wrong with awk or the other commands, if you want to have a “backup” of your software to install on another machine, dpkg --get-selections (without any piping) is the way to go, see https://wiki.debian.org/ListInstalledPackages

Attribution
Source : Link , Question Author : guettli , Answer Author : Broco

Leave a Comment