TipsAndTricks/UsingRPM
The RPM command allows for a number of query operations. The format of the query operations can be used for a number things, such as debugging a package or even exporting information into another format, such as XML.
First in order to perform a query, you want to tell the system to go into query mode:
rpm -q
You then need to add the option to tell it WHAT to query:
To query a single package that has been installed into the database, just add the package name, such as:
rpm -q bash
To query all of the installed packages in the system, add the '-a' option, such as:
rpm -q -a
To query a single package that is not yet installed, add '-p <package name>'
rpm -q -p bash-3.0-r3.i386.rpm
To query multiple packages, just list the packages after the -p:
rpm -q -p bash-3.0-r3.i386.rpm libc6-7-r3.i386.rpm
You can include lists of packages instead:
echo "bash-3.0-r3.i386.rpm" > mymanifest.txt echo "libc6-7-r3.i386.rpm" >> mymanifest.txt rpm -q -p mymanifest.txt
(Note: there is a minimum size of the file for RPM to determine this is a manifest file. So it's recommended if this is used in script to always include a header that says something like "# This is a manifest file".
The above operations simply control the 'what', by default the system will just print the package name. (Some versions will print the name and package architecture.)
In order to select the exact items you want to query, you want to use the '--queryformat' (or '--qf') to specify a format. Such as:
rpm -q -p mymanifest.txt --qf "[%{NAME}\n]"
The above duplicates the default. The special characters or parts being used are:
[ ] - this indicates that it should be applied on each package or item (if the item being queried is an iterator, like a filelist.) Including '\n' inside of the brackets means adding a line feed at the end of every item that is queried. %{...} -- this is how you specify the exact field to be queried. To get a list of valid options, you can run: 'rpm --querytags'.
The format of %{...} also allows for select 'type' conversions. Some of the valid types are: octal, shescape, deptype, pgpsig, day, date, and others.
For instance, the typical, 'rpm -q bash --requires' is actually implemented as:
rpm -q bash --qf "[%|VERBOSE?{%{REQUIREFLAGS:deptype}: }:{}|%{REQUIRENEVRS}\n]"
The above indicate that based on the value 'VERBOSE', either %{REQUIREFLAGS:deptype} followed by either {} | %{REQUIRENEVRS}.
| ? and other special operators exist to permit you to do some basic boolean cases. See: /usr/lib/rpm/rpmpopt-* for a bunch of examples.
Another popular example is how to export the whole DB to XML:
rpm -qa --qf "[%{*:xml}\n]"
The '*' says to not only query the main thing, but also iterate over the matching entries (all of them), with the :xml indicating to dump the contents in XML format.