You can show / search a list of installed packages a number of different ways. For example, to search installed packages for FOO, you could do:
Code:
dpkg --get-selections | awk '/\tinstall/ {print $1}' | grep FOO
This works just fine, but it doesn't have any pretty formatting, and it only shows the package names (you can do more complicated things with grep / sed / awk, but that's a bit beyond the scope of normal users).
The following is a python script that tries to make searching for a certain package a bit nicer. You can search only in installed packages (default), or you can search all packages with the -v switch.
Example:
# apt-checkinstalled rdoc
Code:
* rdoc1.8
version: 1.8.6.111-2ubuntu1
summary: Generate documentation from Ruby source files (for Ruby 1.8)
# apt-checkinstalled -v rdoc
Code:
* rdoc (not installed)
* rdoc1.8
version: 1.8.6.111-2ubuntu1
summary: Generate documentation from Ruby source files (for Ruby 1.8)
* rdoc1.9 (not installed)
This is the python code:
Code:
#!/usr/bin/python
import warnings
warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
import os, sys, apt
bash_colors = {
'gray' : '\033[1;30;40m%s\033[0m',
'blue' : '\033[1;34;40m%s\033[0m',
'green' : '\033[1;32;40m%s\033[0m',
'yellow' : '\033[1;33;40m%s\033[0m'
}
def colorize(color, str):
return bash_colors[color] % str
script = sys.argv.pop(0)
def show_help():
sys.exit('Usage: %s [-v] pkg1 pkg2 ...' % os.path.basename(script))
verbose = False
if not sys.argv or sys.argv[0] in ('-h', '--help', '-help'):
show_help()
else:
if sys.argv[0] == '-v':
verbose = True
del sys.argv[0]
cache = apt.Cache()
pkgs = {}
for pkg in sys.argv:
pkgs[pkg] = 1
for item in cache.keys():
if pkg in item:
pkgs[item] = 1
pkgs = pkgs.keys()
pkgs.sort()
for pkg in pkgs:
try:
cand = cache[pkg]
if cand.isInstalled:
print
print ' %s %s' % (colorize('green', '*'), colorize('blue', cand.name))
print ' %s %s' % (colorize('gray', 'version:'), cand.installedVersion)
print ' %s %s' % (colorize('gray', 'summary:'), cand.summary)
else:
if verbose:
print
print ' %s %s (%s)' % (colorize('green', '*'), colorize('blue', cand.name),
colorize('yellow', 'not installed'))
except KeyError:
if verbose:
print
print ' %s %s (%s)' % (colorize('green', '*'), colorize('blue', pkg),
colorize('yellow', 'not in cache'))
print
Happy hacking.
Bookmarks