The other day, a friend of mine ran the following command in an attempt to remove wine and it’s dependants.
Unfortunately for him, the command worked and did end up removing wine with a whole lot of other system critical
packages. In an attempt to resuscitate his computer, he tried rebooting it and in the moments that ensued, I can
swear I saw the living daylights sucked out of his face. But every cloud has a silver lining and despite the fact
that we could not boot into X, we did have shell access.
Fortunately for us, apt maintains a log of all the operations it performs in /var/log/apt/term.log and while
it would have been easy to just manually make a list of programs that were uninstalled, it wasn’t half-assed
enough. Instead, we summoned the Gods of the shell to aid us in
this conquest.
We began by getting the relevant portion of the log file to another file (while it is possible to work with the
same log file, we didn’t want to go through the hassle of using sudo every time we had to access the log file.)
I’ll admit, we cheated a bit by using 177 and while it could have been programmed easily using grep, we were more excited about what we had to do next to really worry about making all of this reproducible.
As you can see above, apt logs all removed programs as *Removing
The carat in grep’s argument signifies that we want all lines starting with the word ‘Removing’. Also,
we use awk to obtain the second field of the lines (an astute reader may suggest alternatives like
cut as well).
Now that we had a list of the uninstalled programs, all that is left is feeding this list to apt for
installation.
And viola! All was back to normal in Linux Land.