Srijan R. Shetty bio photo

Srijan R. Shetty

Email Twitter LinkedIn Github RSS

The other day, a friend of mine ran the following command in an attempt to remove wine and it’s dependants.

$ sudo apt-get purge wine *

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.)

$ sudo tail -177 /var/log/apt/term.log > temp.log

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.

Log started: 2015-05-01  10:36:15
Removing cmake
Processing triggers for man-db
Log ended: 2015-05-01  10:36:29

As you can see above, apt logs all removed programs as *Removing *, so a simple **grep** search would list all the lines containing the removed programs and the second field of these lines would get us a list of all the programs which were removed by this operation.

$ cat temp.log | grep '^Removing' | awk '{print \$2}'

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.

$ cat temp.log | grep '^Removing' | awk '{print \$2}' | xargs sudo apt-get install -y

And viola! All was back to normal in Linux Land.