How to "grep" a rpm?

Find a file provides by rpm is easy

dnf provides *bin/vim

or

yum whatprovides *bin/vim

If you have rpm installed or yum..but suppose I want to know the content of rpm for exact file.
For example

grep -ril bin/vim v*
vim-9.0.1572-1.2.ppc64.rpm

Too easy

but I want to know the EXACT package which contain bin/sh, how to do?
Using this return the packages which have bin/sh also as a depend.
I want to know the exact package which provides bin/sh as a file.

grep -ril bin/sh v*
v4l-conf-3.107-2.10.ppc64.rpm
vacation-1.2.7.1-13.3.ppc64.rpm
vim-9.0.1572-1.2.ppc64.rpm
virt-what-1.25-1.2.ppc64.rpm
vpnc-0.5.3r550-3.12.ppc64.rpm
vsftpd-3.0.5-10.1.ppc64.rpm
Asked By: elbarna

||

rpm -qf will tell you what package owns a file.

e.g.

% rpm -qf /bin/sh
bash-4.2.46-35.el7_9.x86_64

% rpm -qf /bin/vim
vim-enhanced-7.4.629-8.el7_9.x86_64

If a package isn’t installed, then you can see the contents with rpm -qlp e.g

% rpm -qlp binutils-2.27-44.base.el7.x86_64.rpm | head
/usr/bin/addr2line
/usr/bin/ar
/usr/bin/as
/usr/bin/c++filt
/usr/bin/dwp
/usr/bin/elfedit
/usr/bin/gprof
/usr/bin/ld
/usr/bin/ld.bfd
/usr/bin/ld.gold

From that it would be simple to create an "index" file of the contents of all the rpms in a directory, and then just grep that index.

An off the top of my head example:

% for a in *.rpm
do
  rpm -qlp "$a" | sed "s/^/$a:/"
done > index

% grep :/usr/bin/sh index
bash-4.2.46-34.el7.x86_64.rpm:/usr/bin/sh

Note I had to search for /usr/bin/sh because /bin is a symlink to /usr/bin

Answered By: Stephen Harris

The very first command in your question with any variation of *bin/<file> gives exactly what you are asking for:

yum provides *bin/vim

yum provides /bin/vim

yum provides /usr/bin/vim

vim-enhanced-2:8.2.2637-20.el9_1.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : @System
Matched from:
Filename    : /usr/bin/vim

vim-enhanced-2:8.2.2637-20.el9_1.x86_64 : A version of the VIM editor which includes recent enhancements
Repo        : appstream
Matched from:
Filename    : /usr/bin/vim

yum can be swapped for dnf. That returns the package whether it is installed or not.

If it’s for a file contained by a package that is actually installed:

rpm -qf /bin/sh
bash-5.1.8-6.el9_1.x86_64

rpm -qf /usr/bin/vim
vim-enhanced-8.2.2637-20.el9_1.x86_64
Answered By: Nasir Riley
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.