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:
# rpm -qa --qf "%{NAME}"
redhat-menusglibccracklibgdbmgmplibacllibjpeglincpcreshadow-
utilslibtermcapfreetypeinfofileutilspsmiscntpmountcracklib-dictskrb5-libscyrus-
saslusermodeXftlibpnglibxmllibbonobopythonpygtk2pyxf86configredhat-config-
usersredhat-config-keyboardrpm404-pythongnome-vfs2libgnomeuiashbind-utilscyrus-
sasl-plaindos2unixethtoolfingergroffautofskbdconfiglesslibtool-
libslockdevmailcapMAKEDEVmouseconfignetpbmntsysvORBitpartedppppsutilsrdaterhnlibrp
mrshsetuptoolstatserialtarlilopciutilstimeconfigunzipkernel-pcmcia-
csanacronXFree86
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"
redhat-menus
glibc
cracklib
gdbm
gmp
libacl
libjpeg
linc
pcre
shadow-utils
libtermcap
freetype
info
fileutils
psmisc
ntp
mount
cracklib-dicts
krb5-libs
cyrus-sasl
usermode
Xft
This command provides a custom query that is essentially the same as the rpm -qa command. 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.
Cross Reference
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"
redhat-menus noarch-redhat-linux-gnu
glibc i686-redhat-linux-gnu
cracklib i386-redhat-linux
gdbm i386-redhat-linux-gnu
gmp i386-redhat-linux-gnu
libacl i386-redhat-linux-gnu
libjpeg i386-redhat-linux
linc i386-redhat-linux-gnu
pcre i386-redhat-linux
shadow-utils i386-redhat-linux-gnu
libtermcap i386-redhat-linux
freetype i386-redhat-linux-gnu
info i386-redhat-linux-gnu
fileutils i386-redhat-linux-gnu
psmisc i386-redhat-linux
ntp i386-redhat-linux-gnu
mount i386-redhat-linux-gnu
cracklib-dicts i386-redhat-linux
krb5-libs i386-redhat-linux-gnu
cyrus-sasl i386-redhat-linux-gnu
usermode i386-redhat-linux-gnu
Xft i386-redhat-linux-gnu

4.2.10.1. Working With Query Format Tags

To build queries with the --queryformat option, you need to know what tags you can use. To list the names of the available query format tags, use the --querytags option, which returns a large set of tags, truncated here for space:
# rpm --querytags
NAME
VERSION
RELEASE
SUMMARY
DESCRIPTION
BUILDTIME
Each of these tags also has a version with a RPMTAG_ prefix, such as RPMTAG_NAME. You can use this tags with or without the RPMTAG_ prefix. For example:
$ rpm -q --qf "%{RPMTAG_NAME}\n" sendmail
sendmail
Note how this command uses the –q option to query for one package, instead of –qa to query for all packages. You can use query formats with any of the rpm queries.
The next section cover the available tags based on the type of the information stored in the tag.