2012-12-18

RPM basics: package queries

Since migrating to openSUSE, I'm having to get used to RPM. It has a lot going for it, actually. Today, we'll look at how to use rpm (i.e., the program) to query the package database.

Of course, although rpm can be used to install, upgrade, remove, etc. packages, in this day and age we typically use higher-level tools, such as zypper, for this. The query functions of rpm are still useful, however:


rpm -q

In its most basic form, the rpm command's query function simply looks up the argument in its database of installed packages:
$ rpm -q core-utils
package core-utils is not installed

If the argument matches an installed package, rpm -q returns the package's full name:
$ rpm -q coreutils-8.16
coreutils-8.16-5.8.1.x86_64

(For more complete information on a package, use rpm -qi.)

rpm -qa

List all packages currently installed (NOTE: produces lots of output):
$ rpm -qa

Update history: list all packages in the reverse order in which they were installed/updated (i.e. latest installed packages listed first). This is a very useful command for troubleshooting if I suspect that a bug was caused by a package update:
# rpm -qa --last | head
# ### N.B. only last installation/update of a given package is shown

Find out how many packages are installed:
$ rpm -qa | wc --lines
1903

Find out which packages containing the string "kernel" are installed:
$ rpm -qa | grep kernel
kernel-desktop-3.4.11-2.16.1.x86_64
kernel-firmware-20120719git-2.9.1.noarch

List packages by vendor:
rpm -qa --qf '%{NAME} %{VENDOR}\n' | grep NAME_OF_VENDOR

rpm -qf

Find out which package contains a given file (note that the argument has to be a file currently existing in the filesystem):
$ rpm -qf `which ls`
coreutils-8.16-5.8.1.x86_64

Here's what rpm -qf gives for a non-existent file:
$ rpm -qf ls
error: file /home/smithfarm/ls: No such file or directory

Here's what rpm -qf gives for a file not in any package:
$ touch bubba
$ rpm -qf bubba
file /home/smithfarm/bubba is not owned by any package

rpm -qi

Get information on a package (note that there are several possibilities for the package name: in other words, all three of the following commands produce the same output):
$ rpm -qi kernel-desktop-3.4.11-2.16.1
$ rpm -qi kernel-desktop-3.4.11
$ rpm -qi kernel-desktop

rpm -ql

List files contained in a package:
# rpm -ql PACKAGE_NAME

All of the above queries concerned installed packages. You can also query uninstalled packages:

rpm -qp

From the manpage: "Query an (uninstalled) package PACKAGE_FILE. The PACKAGE_FILE may be specified as an ftp or http style URL, in which case the package header will be downloaded and queried." (Actually, if you're getting this deep into rpm, you should really read the manpage.)

rpm -qpl

The same as -qp, but additionally list the package contents.

rpm -V PKGNAME

Lists differences between what is in the filesystem and what is in the package. Details here.

rpm -q --whatrequires PKGNAME

Lists packages that require this package (i.e. packages that this package is a dependency of).

rpm -q --scripts PKGNAME

List contents of RPM scriptlets (%pre, %post, etc.)

1 comment:

  1. If you are looking for a file, for example libXm.so.3 and you are not sure if it in the package openmotif-libs-32bit or openmotif22-libs-32bit you can your command:
    rpm -qpl ~/openmotif22-libs-32bit-2.2.4-189.1.x86_64.rpm

    ReplyDelete