Skip to main content

Locating packages installed with apt in your bash history

So, I'm backing up my computer for reinstall, and I need a list of what packages I've installed so I can install them again.

I could do it dumping dpkg --get-selections, but I don't want to mess with packages status (installed, uninstalled, pending), and just reinstall exactly the pacakges I installed manually.

So, I need:

  • Listing all packages I've installed with apt or apt-get on my history
  • Cutting all the junk and leaving only the name of the packages
  • Important: Finding if those packages are still installed. Maybe I uninstalled them later!

WARNING: This is a quick and dirty recipe, take a look and test the individual commands before using it!

So this is the recipe I used:

for i in `history| \
          grep -E "apt install|apt-get install"| \
          grep -v grep| \
          tr -s " "| \
          cut -d " " -f 5-| \
          tr " " "\n"| \
          sort| \
          uniq`
do
 if
    dpkg -s ${i} > /dev/null 2>&1
    then echo ${i}
 fi
done

You might need to modify the -f 5- to -f 6- if you use sudo to install.

You can pipe that into a file and then reinstall like this:

for i in `cat packages.txt`; do echo -n "${i} "; done

And add that output to an apt install command.

Cheers!