18

I was given access to a server with 50+ php rpms installed. I'm trying to remove them all.

Basically, I'm trying to combine these two commands:

rpm -qa | grep 'php'

and

rpm --erase

I know a little about pipes and redirection, but I don't see how to use them for this purpose. Please help.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
jerry
  • 2,743
  • 9
  • 41
  • 61

5 Answers5

39

Using yum

List and remove the indicated packages and all their dependencies, but with a y/N confirmation:

yum remove 'php*'

To bypass the confirmation, replace yum with yum -y.

Using rpm

This section builds upon the answers by twalburg and Ricardo.

List which RPMs are installed:

rpm -qa 'php*'
rpm -qa | grep '^php'  # Alternative listing.

List which RPMs which will be erased, without actually erasing them:

rpm -e --test -vv $(rpm -qa 'php*') 2>&1 | grep '^D:     erase:'

On Amazon Linux, you may need to use grep '^D: ========== ---' instead.

If the relevant RPMs are not listed by the command above, investigate errors:

rpm -e --test -vv $(rpm -qa 'php*')

Erase these RPMs:

rpm -e $(rpm -qa 'php*')

Confirm the erasure:

rpm -qa 'php*'
Asclepius
  • 57,944
  • 17
  • 167
  • 143
  • 2
    The `grep` command doesn't pick up anything on my Amazon Linux instance, I think it should be looking for `'^D: ========== ---'` instead. Also (because this is non-obvious), in the case of multiple wildcards (e.g., LibreOffice), the query command would look like `rpm -qa 'libreoffice*' 'libobasis*'`. – Parker Jun 07 '17 at 13:13
9

The usual tool for this job is xargs:

rpm -qa | grep 'php' | xargs rpm -e

This will call rpm -e with all packages named in the standard input of xargs as arguments.

thkala
  • 84,049
  • 23
  • 157
  • 201
0

Another option is to use the output of rpm -qa | grep ... in the rpm --erase command directly:

rpm --erase `rpm -qa | grep php`

Maybe not for the php case you're citing, but the xargs approach might possibly run into issues if it decides to split the list into several invocations of rpm -e and the first list contains packages that are dependencies of packages in subsequent lists. Of course, if you're removing that many packages all at once, you might have other things that you need to consider...

twalberg
  • 59,951
  • 11
  • 89
  • 84
0

to list:

rpm -qa | grep 'php'

to remove instaled listed and filtrated:

rpm -e $(rpm -qa |grep 'php')
glglgl
  • 89,107
  • 13
  • 149
  • 217
Ricardo
  • 17
  • 1
0

I had this today. Using --justdb and --noscripts rpm parameters wasn't sufficient without the --allmatches, and that's it.

[root@localhost ~]# rpm -ev --allmatches --justdb <the-package-name>

https://mcvictech.blogspot.com/2011/10/rpm-error-specifies-multiple-packages.html

Jcc.Sanabria
  • 629
  • 1
  • 12
  • 22