dpkg and apt

(Please note, these are quite old notes about dpkg and comp. For example, I think that apt now tracks the packages also.)

I recommend you to use aptitude instead of apt-get, since it keeps track of the packages that were installed as a dependency only (thus removing those extra packages, when the parent package is removed). But anyway, here is a list of handy commands for package management in Debian.

list

  • List the version of an installed package
 apt-cache policy package | grep Installed
  • Show which package contains somefile
 dpkg -S `locate somefile`
  • Search a string
 apt-cache search string
  • List packages containing a certain string in its name, version or description
 COLUMNS=120 dpkg -l | grep string
  • Use search patterns with aptitude
 aptitude search '~n ^atla ~n doc$'
  • List all the files in a package
 dpkg -L package
  • Show all packages that the package depends on
 apt-cache depends package
  • Show all packages that the package reverse depends on
 apt-cache rdepends package

operations

  • Install a package from a particular release
 sudo apt-get install -t unstable package
  • Remove also the config
 sudo apt-get --purge remove package
  • Debian way to configure: update alternatives
 sudo update-alternatives --config java

status

  • Tell the status of the package
 dpkg --get-selections package
  • Put a package on hold
 echo package hold | sudo dpkg --set-selections
  • Take a package off hold
 echo package install | sudo dpkg --set-selections
Note how impractical those commands are. The aptitude command would simply be
 sudo aptitude (un)hold package
  • Put the package states into a file
 dpkg --get-selections > present-selections.txt
  • Edit the file, then restore
sudo dpkg --set-selections < edited-selections.txt
sudo aptitude update && sudo aptitude upgrade

Note: Nowadays aptitude has hold and unhold implemented
sudo apitude [un]hold  package

cleaning

  • debian-goodies contains a tool "dpigs", which displays the largest packages installed
shell:~$ dpigs
87608 sun-j2re1.5
76728 tetex-base
59512 tetex-doc
54692 acroread
50736 acroread-plugins
45844 linux-image-2.6.16-2-686
45168 linux-image-2.6.17-1-686
44016 linux-image-2.6.15-1-686
42412 tetex-extra
38504 linux-source-2.6.15
  • Remove orphaned packages
 deborphan | xargs sudo apt-get remove --purge --assume-yes
or
 apt-get --purge remove $(deborphan --guess-all | grep -v libc6-i686)
Note: deborphan does not know, if the package was installed by you (therefore having perhaps no dependencies), so be careful when using the above commands.

One can also use orphaner, a frontend for deborphan for finding orphaned packages.

The packages debfoster and localepurge are useful to free wasted disk space. Note: debfoster is now officially deprecated. Use aptitude instead.

If you want to clean your system (after letting apt-get collect all the dependencies during the years since installing Debian), you can use debfoster. Run debfoster and answer the questions (for a couple of hours for the first time). Remove what you don't need. Then execute sudo debfoster2aptitude (in /usr/sbin/debfoster2aptitude), answer questions, then start using aptitude. The apt interface aptitude can keep track of automatically and non-automatically installed packages.

Also programs symlinks and cruft may be useful in maintaining the system. symlinks finds broken and messy symbolic links and cruft examines all files in the system:

  • Examine all files and report files without a properly installed package
 sudo cruft -r cruftreport.txt
Look also at Debian security scanner debsecan.