Packages installed on a Debian/Devuan machine

Finding out which packages are installed on a machine which is currently running is easy. Any of the following commands will tell you:

# dpkg-query -l
# apt list --installed
# aptitude search ~i

Sometimes, however, you have a (pretty complete, I hope) backup of a machine which is no longer running, and you want to find out which packages were installed on it, but it's not a simple matter (or you just don't want to bother) to restore the backup somewhere and restart it.

To find out what was installed without having the machine running, try any of the following:

# cat /pathtobackup/var/lib/dpkg/status
# dpkg --admindir=/pathtobackup/var/lib/dpkg --get-selections "<pattern>"
# dpkg-query --admindir=/pathtobackup/var/lib/dpkg -l

Thanks to my friends at the Devuan mailing list for pointing me in the right direction with this.

If your idea of a backup is simply the user data and none of the system files, then sorry, the above is simply not possible. I recommend you compare the disk space occupied by your system files, and the space occupied by the user files you're backing up, and then decide to back up everything, and rest easy that nothing is now lost to oblivion. I can't remember the last time I saw a machine whose system files took up more space than the user data (unless you include things which get installed by ansible, puppet or similar, and are essentially stateless clones, which need no backups, since they're just replaceable stateless clones).

Packages which were manually installed

It's often helpful to know which packages were installed by the user, rather than being standard system packages, or dependencies for other things (which may in turn have been installed automatically, or by the user).

Here's my best attempt so far at finding out what the user has manually installed:

aptitude search ~i | grep "^i.[^A]" | awk '{ print $2 }' | while read target
do
  [ "`apt-cache rdepends --installed $target | wc -l`" = "2" ] && echo "$target"
done

The above is inspired by something similar but which doesn't exclude the automatically-installed packages.

An improvement

The above bash snippet fails to list packages which were manually installed, but contain a circular dependency, for example audacity:

$ apt-cache rdepends --installed audacity
audacity
Reverse Depends:
  audacity-data

$ apt-cache rdepends --installed audacity-data
audacity-data
Reverse Depends:
  audacity

So, audacity does not get listed because it depends on audacity-data, and vice versa.

If we check to see whether the dependencies of audacity depend in turn (only) upon audacity, then we can get a better list of manually-installed packages:

#!/bin/bash

# Find all the installed packages, discard those installed automatically
# or which depend on some other package (other than ones which in turn depend on them)

debug=0

# Find all installed packages which don't have the Automatic flag set
aptitude search ~i | grep "^i.[^A]" | awk '{ print $2 }' | while read target
do
  [ "$debug" -ge "1" ] && echo "Package $target is installed" >&2
# For each one, find its reverse dependences
  number=`apt-cache rdepends --installed $target 2>/dev/null | uniq | grep -v "\(\b$target\b\|Reverse Depends:\)" | while read depend
  do
    [ "$debug" -ge "2" ] && echo "Package $depend depends on $target" >&2
    apt-cache rdepends --installed $depend 2>/dev/null | uniq | grep -v "\(\b\($depend\|$target\)\b\|Reverse Depends:\)"
  done | wc -l`
  [ "$debug" -ge "2" ] && echo "N=$number"
  [ "$number" -eq "0" ] && aptitude -w 200 search "^$target$" | grep "^i   $target "
done

Go up
Return to main index.