APT Package Management – How to Revert Accidental `dpkg –clear-selections`

aptdpkgpackage-management

I wanted to "purge" all packages that were "removed" only. After skimming through the manpage of dpkg, I thought the --clear-selections parameter would do the trick. So stupid me typed it in with sudo, and afterwards I checked the selection states again and was shocked.

dpkg has selected all (non-vital) installed packages to get uninstalled!

This was of course my fault, but definitely not intended. So, how do I revert this?

I still have the terminal window open where I ran dpkg --get-selections | grep deinstall both before (85 packages) and after (2614 packages) the accidental command.

What I need now is to subtract the first (old) deinstall list from the second (current) one and mark all those packages as wanted again. Setting them to manual will not be good though, as that would affect hundreds of libraries which make no sense if installed without their applications. So we have to set it to auto and then I probably have to manually find out the top level packages to be selected as manual. Or can something help me there?

Info: I have two separate text files, one containing the first --get-selections output, one the second. They contain only lines with the syntax:

package-name         deinstall

where package-name is, well, the actual package name, and the space in between is an undefined number of tabs.

Update: I just found the output of dpkg --get-selections from before the accident without any grep filtering! Only two packages were installed after that, I can fix them manually later.

Please help me to restore my previous package selections!

Best Answer

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.

Related Question