On my Ubuntu desktop I often find myself installing many packages to test new software, and then removing unwanted packages several hours later. The Debian package manager "aptitude" tends to remove packages that were pulled in as dependencies. For good reasons it does not always remove configuration files (e.g. they were modified).
While it does not hurt to have excess config files laying around, they build up over time, and can easily be removed.
The one-line shell command sudo aptitude purge $(dpkg -l | grep '^rc' | awk '{print $2}') will completely remove all packages that left configuration files after the package was removed.
A breakdown of this command:
- dpkg -l lists all packages registered with the package manger.
- | grep '^rc' takes the output of the previous command and only passes lines that begin with 'rc'.
- | awk '{print $2}' further changes the output by only displaying the second space-delimited column on each line.
- $(...) takes the output of evaluating the contents of the parenthesis and includes it on the command line.
- sudo aptitude purge ... will completely remove specified packages.
A quick example would make this more clear. Let's assume I have installed mysql-en-us, and then removed it, but not its configuration files. The output of dpkg -l | grep '^rc' would be:
myspell-en-us 1:2.4.0-2ubuntu4 English_american dictionary for myspell
The output of dpkg -l | grep '^rc' | awk '{print $2}' would be:
myspell-en-us
Thus when I enter sudo aptitude purge $(dpkg -l | grep '^rc' | awk '{print $2}') it is equivalent to sudo aptitude purge mysql-en-us.
I'm sure there are other methods of achieving the same thing using other means. I've found that aptitude removes the dependencies and configuration files more often than apt-get, but this extra step helps to eliminate even more unnecessary files.