Working on a CentOS 6.9 machine and I'm more than a little rusty on my bash scripting. Before I get into the specifics of what I'm trying to do, here's my code so far for reference:


yum list all > all_packages.txt
len=`expr length "$package_name"`

while IFS= read -r line || "$line"; do
if [[${line:0:$string = $package_name}]]
then <>

My end goal is to let a user define a package to uninstall, save the currently installed packages to a text file, compare the lines in that file to see which lines correspond to the specified package, save that as a new text file and then let a "yum remove" command iterate through that text file to delete the package in its entirety.

My approach at the moment is to get the package the user wants to delete, get the length of the corresponding string and compare between the first X characters of each line and the package itself--if they don't match, I'll delete that line and move on to the next one.

My problem at this stage is that I don't know how to remove the lines that don't match the specified package. From what've seen online it can be done with "sed" or "grep", but I haven't gotten much past that.

Thanks in advance for all the help! I deeply appreciate it!

  • 1
    yum already does this for you, and will tell you if you are trying to remove a package that is not installed. What is the problem you are actually trying to solve here?
    – DopeGhoti
    Apr 6, 2018 at 16:10
  • The problem I've bumped into is that yum doesn't always seem to remove every associated installation related to a package. For instance, while attempting to clear cut everything on an installation of Eclipse I ran sudo yum remove eclipse and found eclipse packages still left over afterwards. I'm hoping that by doing this I can identify every package and nix them at once.
    – P. Monroe
    Apr 6, 2018 at 16:22
  • yum remove packagename* ? Wildcards can be used. package-cleanup --quite --leaves ? Apr 6, 2018 at 17:23

1 Answer 1


Beware the XY Problem.

You want to remove packages that were only installed as dependencies, which yum can do. What if eclipse installed other packages as dependencies, but those packages didn't include "eclipse" in the name? String-matching won't help there.

From man yum, yum autoremove will remove all packages that were not installed manually by the user and are no longer required by anything. yum autoremove mypackage will remove just the given package and its no-longer-needed dependencies.

To autoremove any time you use yum remove, add "clean_requirements_on_remove=1" to your /etc/yum.conf .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .