Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a scenario where I'd like to remove a set of packages that may or may not be installed, and I'd like apt-get to remove those that are and silently ignore those that aren't. Something like:

apt-get remove foo bar baz

which, if foo and bar were installed but baz was not, would remove foo and bar without complaining about baz. Is there a way to do this?

Things I've tried that haven't worked, with cups-dbg as my scapegoat actually-installed package to be removed:

jcp@a-boyd:~$ sudo apt-get remove -y cups-dbg bogus-package
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package bogus-package

jcp@a-boyd:~$ sudo apt-get remove --ignore-missing cups-dbg bogus-package
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package bogus-package

jcp@a-boyd:~$ sudo apt-get remove --fix-broken cups-dbg bogus-package
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package bogus-package

I know I could do this with a shell script and some dpkg --list magic, but I'd like to avoid any complexity that's not absolutely necessary.

share|improve this question
add comment

migrated from stackoverflow.com Dec 13 '12 at 14:45

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 0 down vote accepted

Is falling back to lower-level tool such as dpkg an option?

dpkg --remove foo bar libperl-dev
dpkg: warning: ignoring request to remove foo which isn't installed
dpkg: warning: ignoring request to remove bar which isn't installed
(Reading database ... 169132 files and directories currently installed.)
Removing libperl-dev ...
share|improve this answer
    
I should have mentioned that the packages to be removed can have reverse dependencies, and I'd like those to be removed too. Apt-get is therefore much better than dpkg, but I'll accept your answer since it seems that there isn't really a better way to do this. –  javawizard Dec 14 '12 at 19:22
add comment

I use apt-get remove --purge (aka apt-get purge) for the dependency following with a list of packages. To handle packages that don't exist I filter out packages that are not installed with the following script.

pkgToRemoveListFull="cups-dbg bogus-package"
pkgToRemoveList=""
for pkgToRemove in $(echo $pkgToRemoveListFull); do
  $(dpkg --status $pkgToRemove &> /dev/null)
  if [[ $? -eq 0 ]]; then
    pkgToRemoveList="$pkgToRemoveList $pkgToRemove"
  fi
done
apt-get --yes --purge remove $pkgToRemoveList
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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