Every package you install with RPM is recorded in the RPM database. The RPM system includes commands to query this database to find out which packages are installed and to provide quite a few details about these packages.
This chapter covers querying both the RPM database and RPM package files. Both types of query are important:
*Query the RPM database to see what is installed, or not installed, on your system.
*Query package files to see what the files require, as well as what the files provide.
In addition to querying the RPM database, you can use the database to verify packages. Since this database is so important to the management of your Linux system, this chapter covers how to back it up, as well as how to repair a damaged RPM database.
4.1. Querying the RPM Database
In
Chapter 3, Using RPM , you saw that the rpm command usually takes one major command-line option to tell it the operation to perform and a myriad of command-line options to customize the operation. The rpm command may also take the name of one or more RPM package files or the name of one or more installed packages. For example, the rpm –i command performs an installation operation, and the rpm –U command performs an upgrade.
For querying the RPM database, the major command-line option is –q, short for query. This option tells the rpm command to query the RPM database. You can also use the long option --query.
In the last few chapters, you've used the –q option with the rpm command to query just for the presence or absence of installed packages. You can expand the -q option to perform a wide array of queries to find out information about the packages installed on a Linux system.
4.1.1. Querying packages
The basic format of the rpm –q command follows:
rpm –q package_name
You need to provide the name of a package to query. For example:
rpm -q telnet-0.17
This command returns the name of the package, if installed. For example:
telnet-0.17-20
If the package is not installed, you’ll see a message like the following:
package telnet-0.17 is not installed
You can provide the whole package name to the rpm command, which includes the name, the version, and the RPM package number, as discussed in
Chapter 2, RPM Overview . You can also just provide the name and version number, as shown previously, or just the base name of the package.
For example, the following command uses just the base name of the package:
$ rpm -q telnet
telnet-0.17-20
Note
The rpm –q command expects a package name. Although it supports some amount of customized queries, you really need to know which packages you want the rpm command to report on.
You can provide more than one package name; the rpm command reports on each package, as shown following.
$ rpm -q telnet telnet-server
telnet-0.17-20
telnet-server-0.17-20
You need to change the way you query if you want to perform searches when you do not know the full package name in advance. The following section cover options for creating various queries.
4.1.2. Querying everything
Up to now, we have used the rpm command to query only for specific packages. The –a option tells the rpm command to query for all packages. You can also use the longer option, --all, in place of –a.
For example:
rpm -qa
This command returns every package installed on your system, quite a few packages. The packages are returned one per line, as shown following.
words-2-17
kudzu-0.99.23-1
openldap-2.0.11-13
rpm-4.0.3-1.03
kernel-smp-2.4.7-10
quota-3.01pre9-3
expat-1.95.1-7
groff-perl-1.17.2-3
perl-DateManip-5.39-5
perl-libnet-1.0703-6
perl-URI-1.12-5
perl-XML-Parser-2.30-7
perl-XML-Twig-2.02-2
a2ps-4.13b-15
4Suite-0.11-2
XFree86-xfs-4.1.0-3
ghostscript-6.51-12
tcl-8.3.3-65
portmap-4.0-38
bind-utils-9.1.3-4
ftp-0.17-12
micq-0.4.6.p1-2
Note
This output has been modified to meet size constraints. Try the rpm –qa command to see the full output for your system.
There may be over a thousand packages on your system. Even so, the rpm –qa command executes surprisingly fast.
4.1.3. Refining the query
When you query all the installed packages, you get too much output for most purposes, other than to get a general idea of the magnitude of packages installed on your system. But if you cannot remember a package name, there's no real option, other than writing your own RPM query program.
You can take advantage of the power of the Linux shells, though, and the wonderful ability to pipe the output of one command into another to work around this problem. With the large amount of output, you may want to pipe the output to the more or less programs, and display the output one page at a time.
Cross Reference
For more information on the more or less commands, view the online manuals with the man more and man less commands.
Even with more and less, the rpm –qa command outputs too much information to be really useful, unless you can somehow filter the information automatically.
4.1.3.1. Piping the Output To grep
The Linux (and Unix) grep command provides a powerful tool for filtering through lots of textual data. If you pipe the output of the rpm –qa command into the grep command, you have a powerful search engine—Linux--at your fingertips.
For example, if you know that most packages that use the Python scripting language have a py in their names, you can find all these packages by using a command like the following:
rpm -qa | grep py
This command outputs packages such as the following:
python-2.2.1-17
pygtk2-1.99.12-7
pyxf86config-0.3.1-2
rpm404-python-4.0.4-8x.27
python-devel-2.2.1-17
gnome-python2-gtkhtml2-1.99.11-8
orbit-python-1.99.0-4
gnome-python2-canvas-1.99.11-8
gnome-python2-bonobo-1.99.11-8
gnome-python2-1.99.11-8
pyOpenSSL-0.5.0.91-1
rpm-python-4.1-1.06
pygtk2-devel-1.99.12-7
kdesdk-kspy-3.0.3-2
mod_python-3.0.0-10
gnome-python2-gconf-1.99.11-8
libxslt-python-1.0.19-1
python-tools-2.2.1-17
libxml2-python-2.4.23-1
pygtk2-libglade-1.99.12-7
python-optik-1.3-2
kfloppy-3.0.3-3
You can also use the --pipe option to the rpm command, introduced in
Chapter 3, Using RPM . With this option, your command becomes:
rpm -qa --pipe "grep py"
Cross Reference
You can take advantage of some of the options that the grep command supports, including -i for ignoring the case of the string to match, --regexp to pass a regular expression to grep, and -v, to output only those entries that do not match the search string.
Cross Reference
If you are unfamiliar with grep, the online manual pages for the grep command provide a listing of the command-line options available for grep as well as a short tutorial on regular expressions supported by grep.
Table 5-1 lists some of the common package-naming conventions. Remember that these are just conventions, not hard-and-fast rules. You can use these conventions in concert with rpm queries.
Table 5-1 Common Naming Conventions on Linux
4.1.3.2. Querying with Wildcards
In addition to using other Linux commands, the rpm command supports some search options. You can pass a wildcard to rpm –qa (but not just rpm –q, you need the –a to look for all packages). For example:
$ rpm -qa "send*"
sendmail-cf-8.11.6-3
sendmail-8.11.6-3
Note
The quotation marks around "send*" are to prevent the Linux shell from expanding the wildcard character, *, to try to match a list of file names in the local directory. By passing the command-line parameter as "send*", the rpm program gets to see the * character. Otherwise, the shell expands the parameter and the program, rpm in this case, never sees the *.
This command searches for all package names starting with send. You can reverse this with an exclamation mark. For example:
$ rpm -qa '!send*'
This command works sort of like grep –v and searches for all packages that do not start with send.
There are quite a few other Linux commands you can use in pipelines with the rpm –qa command to better filter and display the data, such as wc –l to count the number of packages that you query. You can also use a number of other query options to find out more specialized information from the RPM database.
Cross Reference
If you aren’t familiar with grep or other Linux commands, pick up a Linux tutorial such as (insert shameless plug) Teach Yourself Linux by Steve Oualline and Eric Foster-Johnson, available from Wiley Publishing, Inc.
4.1.4. Finding which packages own files
One of the common problems with package management comes when you want to track a given file on your system back to the package that “owns” the file (that is, the package that, when installed, installed the particular file).
The -qf option tells the rpm command to query for all packages that own a particular file. You can also use the longer option, --file, in place of –f. The basic syntax follows:
rpm -qf filename
For example, the grep command used in previous examples is really a file. (Just about all Linux commands are a file of some sort, be it a shell script or an executable application.) You can use a few Linux commands to determine which package provides this handy program.
First, we need the exact path to the file. For Linux commands, you can use the which command, if the program is in your path. (The grep program must be in your path, or commands with grep will fail.)
Try the following command:
which grep
This command returns the path to grep:
/bin/grep
We can now check which package owns this file with the following command:
# rpm -qf /bin/grep
grep-2.4.2-7
You can also use the Linux back-tick operator to perform this check with one command.
# rpm -qf `which grep`
grep-2.4.2-7
Production: note use of back-quote, `, above. Don’t change to “smart quotes” please. -Eric
If you use the bash shell, you can use the $(command parameters) syntax in place of the back tick, or `, characters. For example:
# rpm -qf $(which grep)
grep-2.4.2-7
If no package owns a given file, you’ll see output like the following:
# rpm -qf mail
file mail is not owned by any package
Often, the package that owns a file does not have an intuitive name. The ssh command, for example, is owned by the openssh-clients package, as shown following:
# rpm -qf `which ssh`
openssh-clients-3.1p1-2
As you can see, the name of a command does not always correspond directly to the name of the package that provides that command. This is where the rpm –qf command proves very useful. Otherwise, you would just have to know that OpenSSH is the project responsible for this command.
Symbolic Links
The rpm -qf command follows symbolic links. This was not always true with older versions of the rpm command, but the modern rpm command can now trace package ownership to files placed in linked directories.
For example, the directory /usr/lib/X11 is a link to the real directory, /usr/X11R6/lib/X11. You can track the package ownership of a file in that directory, XKeysymDB, for example, by using the following command:
# rpm -qf /usr/lib/X11/XKeysymDB
XFree86-4.2.0-72
This file, XKeysymDB, really resides in /usr/X11R6/lib/X11.