3

I have to remove all packages installed on a specific day.

My solution is to look up the file /var/log/dpkg.log, find out the entry with the right date, extract the names of the packages and remove them.

Can I be sure sure that, in this way, I will find all desired packages?

Is there another way to do it, in a more secure/formal way?

  • 1
    A better choice would be to look at the apt log files, in /var/log/apt, particularly /var/log/apt/history.log. – Faheem Mitha Feb 28 '14 at 8:29
2

/var/log/apt/history.log is usually better to use for multiple reasons for this, one is that it logs whether a package was installed automatically to satisfy dependencies or not. Say upgrades are installed on the same day and some of them have new dependencies. If you try to remove those, apt will recommend removing the upgraded packages too. You can always remove automatic dependencies of the removed packages after with apt-get autoremove.

One downside of /var/log/apt/history.log is that it only logs packages installed via apt (command line or package manager), not those installed directly with dpkg. Another is that the log is horrific to parse:

date=2014-02-26
awk '$1=="Start-Date:" && $2=="'"$date"'" && got_date="yes" {}
  got_date=="yes" && $1=="Install:" && got_date="no" {
    for(i=2; i<=NF; i++) {
      if( $i !~ /,|\)/ && $(i+2) !~ /^automatic\)/ ) print $i } }' \
  /var/log/apt/history.log |
  xargs sudo apt-get -s remove

Remove the -s from apt-get when you are sure you have what you want. Date format might vary with locale, so check your log to see.

Also, apt-get autremove will not remove packages if another package suggests/recommends them (eg package is installed as a dependency, but kept due to a suggestion). Here is the command to get everything, beware this may remove more than you bargained for (for me this it actually wants to remove my desktop environment!):

date=2014-02-26
awk '$1=="Start-Date:" && $2=="'"$date"'" && got_date="yes" {}
  got_date=="yes" && $1=="Install:" && got_date="no" {
    for(i=2; i<=NF; i++) {
      if( $i !~ /,|\)/ ) print $i } }' \
  /var/log/apt/history.log |
  xargs sudo apt-get -s remove

Again remove -s when happy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.