I am trying to get the distribution name and version number to enter into conky.  I am currently using the following

rpm --query centos-release

resulting in

centos-release 7-4.1708.e17.centos.x86_64

How do I pare that down to just centos 7-4.1708.e17?

After trying all the suggestions I ended up entering this into my conky

${font Roboto:bold:size=8}${goto 95}${color1}Distribution $alignr   ${execi > 60 a=$(rpm --query centos-release)
a=${a#centos-release }
echo "$a"}

with this result

  • Should we assume you meant there to be a hyphen between centos and 7-4 in both instances? Standard RPM names don't have spaces. – Wildcard Jan 17 '18 at 2:45
  • Also, you wrote e17, but I suspect you meant el7. Copying and pasting is far preferable to retyping. – Wildcard Jan 17 '18 at 3:28
  • Do you want the version of the RPM, or the version of the distribution? They are different. – Michael Hampton Jan 17 '18 at 4:35
  • I could not find a way to copy out of the terminal so yes el7. I release spaces are not the norm but as this is just for reference I find it much easier to read so I would like the space but not super important. I am not 100% sure on the version of the RPM or distribution but the outcome I am looking for is centos 7-4.1708.el7 – Starblight Jan 17 '18 at 7:09

With sed:

$ rpm --query centos-release | sed 's/^centos-release//;s/\.centos.*//'

With only shell:


a=$(rpm --query centos-release)
a=${a#centos-release }
echo "$a"

A more general approach to printing the names and details of RPM packages in customized formats is the --qf or --queryformat option to the rpm command.

To quote man rpm in part:

Query formats are modified versions of the standard printf(3) formatting. The format is made up of static strings (which may include standard C character escapes for newlines, tabs, and other special characters) and printf(3) type formatters. As rpm already knows the type to print, the type specifier must be omitted however, and replaced by the name of the header tag to be printed, enclosed by {} characters. Tag names are case insensitive....

You can see the list of tag names that rpm knows about by running rpm --querytags. It's a long list, so I won't reproduce it here in full. However, some tags of interest include:


Unfortunately for your exact use case, the name of the package in question is centos-release, not centos as desired, and the release is 4.1708.el7.centos.

So the closest you can come to the output desired with pure rpm query formatting, is:

rpm -q centos-release --qf 'centos %{v}-%{r}\n'

(Note the use of a static string for "centos" rather than the %{n} tag, which would expand to "centos-release". Also note the space, which you have in your desired output, although that may be a typo.)

On my test box this command produces:

$ rpm -q centos-release --qf 'centos %{v}-%{r}\n'
centos 7-2.1511.el7.centos.2.10

If your release has a dash (or space) between centos-release and the number you can...

  • replace "centos-release-" with "" using sed (s/^centos-release.//) and
  • replace ".centos.*" with "" (s/\.centos.*//)
  • stdout shows only the release

$ rpm --query centos-release

$ rpm --query centos-release | sed 's/^centos-release.//;s/\.centos.*//'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.