Revert accidental `dpkg --clear-selections`

Solution 1:

To set all packages currently selected to deinstall back to install, you run the following two commands:

sudo dpkg --get-selections | awk '/deinstall/ {printf "%s\t%s\n",$1,"install"}' > packages
sudo dpkg --set-selections < packages

The first command finds all packages marked with deinstall and writes a list of packages like this

package-name         install

The second command corrects the packages.


If the full output of dpkg --get-selections from before the accident is given, you can simply restore all selections with the command

sudo dpkg --set-selections < packages

where packages is the name of the file holding the old output.