Product SiteDocumentation Site

4.2.10. Creating custom queries

The --qf or --queryformat option allows you to create custom queries with the rpm command, although in a rather difficult manner. You need to pass a query format string, the syntax of which originates with the C printf function and requires precision.
The basic syntax of the query format is %{tag_name}. (The percent sign is about the only part that comes from the C printf function.) You can combine tag names to display more than one item per package. You can also add formatting options following C language conventions.
For example, to list all package names, use a command like the following (the output is abbreviated):
rpm -qa --qf "%{NAME}"
libxml2tcp_wrappers-libstracerouteperl-Config-Tinylibsampleratefedora-icon-themefftw2dvb-appsreport-plugin-localsaveglib2-develqwtintltoolperl-Locale-Maketext-Gettextliblastfmxorg-x11-fonts-ISO8859-1-100dpilibpng-devellibgeotifffontconfigPackageKit-glibshared-mime-infoxorg-x11-drv-sisusbxmlgraphics-commonsopenoffice.org-graphicfilterm17n-db-oriyadejavu-sans-fontslibselinux-pythonapr-utilxorg-x11-drv-cirruslibsilcmysql-serverpnm2ppanssmythmusicdbus-glibcyrus-sasl-libfedora-release-notesekigaperl-Net-SSLeayperl-HTML-Parsermingettyphp-clixorg-x11-drv-nvidia-libspatchgnome-games-extra
This command used the simplest format, which is just the value of the tag in the package headers, in this case the package names. Because we used no other formatting, this command outputs all the package names smashed together. To deal with this problem in the output, you can place a \n, the C language convention for a newline character, at the end of the format string. This fixes the output considerably.
For example (showing just the first few entries):
rpm -qa --qf "%{NAME}\n"
libxml2
tcp_wrappers-libs
traceroute
perl-Config-Tiny
libsamplerate
fedora-icon-theme
fftw2
dvb-apps
report-plugin-localsave
glib2-devel
This command provides a custom query that is essentially the same as rpm -qa. You’ll likely not use this command in favor of the simpler rpm option, but you can use this example as a guide for creating your own custom queries.

popt aliases

A great many of the command-line options to the rpm command are defined as popt aliases. These popt aliases define the rpm command-line options in terms of longer query format strings. See Chapter 20, Customizing RPM Behavior for more information on popt.
You can add items to the query string and use C language conventions for formatting and controlling the amount of space provided for each item output. For example, the following command prints the name and platform for all packages, showing the first few entries, formatted with 20 characters for each item:
rpm -qa --qf "%-20{NAME} %-20{PLATFORM}\n"
libxml2              x86_64-redhat-linux-gnu
tcp_wrappers-libs    i686-redhat-linux-gnu
traceroute           x86_64-redhat-linux-gnu
perl-Config-Tiny     noarch-redhat-linux-gnu
libsamplerate        x86_64-redhat-linux-gnu
fedora-icon-theme    noarch-redhat-linux-gnu
fftw2                x86_64-redhat-linux-gnu
dvb-apps             x86_64-redhat-linux-gnu
report-plugin-localsave x86_64-redhat-linux-gnu
glib2-devel          x86_64-redhat-linux-gnu