Edit History Actions

list of explicitly installed packages

Ubuntu: list of explicitly installed packages

To generate a list of explicitly installed packages is not a trivial task in (K)ubuntu. Not if you really want to see all packages you explicitly requested for installation and don't want to see any packages which were implicitly installed as a dependency.

Why?

When installing Ubuntu on another machine, you could simply wait for some situation where you'll think: "oh, I had this nice fancy program to read comics but I don't remember the name, it simply worked, when I clicked on a *.cbr file". And then you have to search the apt-cache or start your old computer or ask a search engine. But wouldn't it be cool, if it is already there?

Another situation could be: You installed Ubuntu on the machine of your girl-/boyfriend, and then (s)he calls: "Hi, Ubuntu looks nice, but you*ube is not working". You would say: "yea, you have to install (k)ubuntu-restricted-extras". And (s)he will ask: "eh, what's that?"

Both problems could have been avoided if you had installed all the tools you are used to have installed on your old/own machine. The answer could be: Just generate a package list with

dpkg-query -W -f='${Package}\n' > packages.list

or something like that and install that on the other machine with

xargs -a "packages.list" sudo apt-get install

But your girl-/boyfriend doesn't need all that compiler stuff and your new machine should run Kubuntu and not Ubuntu, so you will have to select what to install and what not. And a cool help on that task would be a nice list of packages which you installed previously by hand.

How?

There appears to be a file, in which is stored when a file was installed automatically (as a dependency!?):

/var/lib/apt/extended_states

You can get a list of all packages installed with

dpkg-query -W -f='${Package}:${Status}\n' | grep installed | cut -d ":" -sf 1 > packages.list

then remove all automatically installed packages from that list. The problem is, that there are many packages installed during the installation of (K)ubuntu which are not marked as automatically installed. The following script tries to remove some of these, using the modification dates of the files /var/lib/dpkg/info/*.list. Every one of these files stands for an installed package and the script is removing the packages from the list where the date of the *.list file is used to determine if the package was installed after the installation of (K)ubuntu. So when was my (K)Ubuntu installed? Don't know, you'll have to guess.

Download Script

   1 #!/usr/bin/python
   2 # explist: list explicitly installed packages in Ubuntu/Debian
   3 # Version 0.1 by Pascal Rosin
   4 # Requirements: (K)Ubuntu, Aptitude
   5 # This program is free software: you can redistribute it and/or modify
   6 # it under the terms of the GNU General Public License as published by
   7 # the Free Software Foundation, either version 3 of the License, or
   8 # (at your option) any later version.
   9 # This program is distributed in the hope that it will be useful,
  10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 # GNU General Public License for more details.
  13 # You should have received a copy of the GNU General Public License
  14 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15 
  16 from optparse import OptionParser
  17 from subprocess import Popen,PIPE
  18 import re
  19 
  20 def main():
  21     usage = "usage: %prog [options]"
  22     parser = OptionParser(usage)
  23     parser.add_option("-d", "--date", dest="date",
  24         help='generate list from packets installed since DATE', metavar="DATE")
  25     parser.add_option("-o", "--outfile", dest="fname",
  26         help="write packetlist to FILE", metavar="FILE")
  27 
  28     (options, args) = parser.parse_args()
  29 
  30     manualpackages = Popen('aptitude -F %p search "?and(?installed,?not(?automatic))"',shell=True,stdout=PIPE).stdout.xreadlines()
  31     manualpackages = [p.strip() for p in manualpackages]
  32 
  33     if options.date:
  34         date = options.date
  35 
  36         prexp = re.compile(r"^.*/(.*)\.list$")
  37         out = Popen("find /var/lib/dpkg/info/ \\( -newermt \"%s\" -name \\*.list \\)"%date,shell=True,stdout=PIPE).stdout
  38         packages = [prexp.match(p).groups()[0] for p in out.xreadlines()]
  39 
  40         manualpackages = list(set(manualpackages) & set(packages))
  41 
  42     manualpackages.sort()
  43 
  44     if options.fname:
  45         outfile = open(options.fname,"w")
  46         for package in manualpackages:
  47             outfile.write(package+"\n")
  48         outfile.flush()
  49         outfile.close()
  50     else:
  51         for package in manualpackages:
  52             print(package)
  53 
  54 if __name__ == '__main__':
  55     main()
explist.py

CategoryUbuntu CategoryLinux