If you've ever thought "there should be a command that does X" for Linux, there probably is. Finding it, however, is not always easy. This is especially true when managing packages on Debian-based systems.
Debian's package tools (
dpkg, the APT suite and utilities like
aptitude) make the basics of installing and managing packages very easy. When you want to do more complex things, however, they're still easy(ish), but the options or tools you want are not as well-advertised.
One thing that is often useful is to know why a package was installed. To find out, we want to use the
aptitude utility, which will provide this very easily and quickly. Use
aptitude why packagename to find out what package requires or suggests the package.
If you want to install packages that have been "kept back," you'll often hear people suggest that you use
dist-upgrade instead of
upgrade. However, a better way to do this -- without carrying a bunch of updates forward that you may not want, is to use
aptitude instead of
Occasionally, you must know what package a file belongs to, or what files are in a package. For a file that's installed, use
dpkg -S filename. For example, if you don't have Sendmail installed and want to know what package owns the symlink for
/usr/lib/sendmail, you can run
dpkg -S /usr/lib/sendmail. In my case, this returns:
What if you want to know what package would install a file? That's a job for
apt-file. Note that this utility may not be installed by default. You'll also need to update its cache by running
apt-file update. Then run
apt-file filename that you want to see. The more specific you can be, the better. If you look for a single string that's likely to be in many filenames (like "vim"), you'll get quite a few results. If you look for something very specific like
/usr/lib/xml2Conf.sh, then it will provide only one result. So if I search for
/etc/apache2/apache2.conf even on a system without Apache installed, it will tell me that the package I'm looking for is
Last, but definitely not least, let's look at saving a list of all installed software. Say you want to do a clean install of Debian (or a Debian derivative) to upgrade rather than
apt-get dist-upgrade, but you don't want to figure out by trial and error what packages you had before -- simply run
dpkg--get-selections, and you'll see a full list of packages that are installed. Here, I also notice that my Linux Mint desktop has more than four times as many packages installed as my Debian server.
But what about restoring the packages? That's easy. Run
dpkg--get-selections > installed-packages.txt. When you have the clean system, run
dpkg--set-selections < installed-packages.txt. Do be sure to back this file up before doing the install, of course.
While I tend to be partial to Debian packages, there's no reason Debian users should have all the fun. Next week, I'll take a look at tips for using RPM and Yum.
Joe 'Zonker' Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at email@example.com and follow him on Twitter.